的ExecuteReader:Connection属性尚未初始化 [英] ExecuteReader: Connection property has not been initialized
本文介绍了的ExecuteReader:Connection属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的ExecuteReader:连接酒店有
尚未初始化
块引用>
我的编码是
保护无效Button2_Click(对象发件人,EventArgs五)
{
SqlConnection的康恩=新的SqlConnection(数据源=硅6\\SQLSERVER2005;初始目录=碎布;集成安全性= SSPI);
SqlDataReader的RDR = NULL;
试
{
// 2.打开连接
conn.Open();
// 3.通行证(从客户选择*,conn)的连接到命令对象
//的SqlCommand CMD =新的SqlCommand;
的SqlCommand CMD =新的SqlCommand(插入时间(项目,迭代)
值('+这.name1 .SelectedValue +,+这.iteration .SelectedValue +') );
//
// 4.使用连接
//
//获取查询结果
RDR = cmd.ExecuteReader( );
//打印每条记录
的客户ID,而(rdr.Read())
{
Console.WriteLine(RDR [0]);
}
}
终于
{
//关闭读者
如果(RDR!= NULL)
{
RDR 。关();
}
// 5.关闭连接
如果(参数conn!= NULL)
{
conn.Close();
}
}
}
}
}
解决方案使用,并通过连接对象:
的SqlCommand CMD =新的SqlCommand(插入时间(项目,迭代)VALUES('+这.name1 .SelectedValue +,+这.iteration .SelectedValue +'),conn);在
ExecuteReader: Connection property has not been initialized.
my coding is
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(); } } } } }
解决方案use this and pass connection object :
SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')",conn);
这篇关于的ExecuteReader:Connection属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文