填写:SelectCommand.Connection 属性 尚未初始化 C#
Fill: SelectCommand.Connection property has not been initialized C#
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con=new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
cmd.CommandText="select id from regtb where id="+Session["id"];
da.SelectCommand = cmd;
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("userlogin.aspx");
}
}
这段代码有什么问题?
谁能帮我解决这个问题
我收到此错误,但我不知道如何更正它。
您需要打开 SqlConnection,然后将其分配给 SqlCommand:
con.Open();
cmd.Connection = con;
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con=new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
cmd.CommandText="select id from regtb where id="+Session["id"];
da.SelectCommand = cmd;
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("userlogin.aspx");
}
}
这段代码有什么问题? 谁能帮我解决这个问题 我收到此错误,但我不知道如何更正它。
您需要打开 SqlConnection,然后将其分配给 SqlCommand:
con.Open();
cmd.Connection = con;