【发布时间】:2011-05-03 06:47:23
【问题描述】:
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 指令来完成。
-
这些答案是正确的。你必须接受。你必须用你创建的连接初始化 sqlcommand 连接属性。
标签: c# asp.net sql-server-2005