ExecuteReader:连接属性有 未初始化。ExecuteReader:连接属性尚未初始化
我的编码是
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI");
SqlDataReader rdr = null;
try
{
// 2. Open the connection
conn.Open();
// 3. Pass the connection to a command object
//SqlCommand cmd = new SqlCommand("select * from Customers", conn);
SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)
values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')");
//
// 4. Use the connection
//
// get query results
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
}
}
由于SqlConnection,SqlCommand和SqlReader对象正在使用非托管资源,因此它们是一次性对象,因此在任务完成时处理它们是一种很好的做法。为了使代码更具可读性,您可以使用using指令来执行此操作。 – Beatles1692 2011-05-03 06:56:20
这些答案是正确的。您必须接受。您必须使用您创建的连接初始化sqlcommand连接属性。 – Saleh 2011-05-03 06:57:53