我得到一个错误,试图查询我的数据库在ASP

本文关键字:查询 我的 数据库 ASP 错误 一个 | 更新日期: 2023-09-27 18:04:53

我试图让SqlDataReader工作,但我得到这个错误:

ExecuteReader: Connection属性未初始化。

我尝试用SqlDataReader = new SqlDataReader();初始化它但是这又给了我一个错误。

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=BETSY''SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True";
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM ShoppingList2, con");
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Close();
        con.Close();
    }

我得到一个错误,试图查询我的数据库在ASP

"SELECT * FROM ShoppingList2, con"

"SELECT * FROM ShoppingList2", con

try this:

using (SqlConnection con = new SqlConnection("Data Source=BETSY''SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True"))
{
    con.Open();
    using (SqlCommand com = con.CreateCommand())
    {
        // sql setup stuff here
        using (SqlDataReader reader = com.ExecuteReader())
        {
            // read data here
        }
    }
}