C# “executenonquery 连接属性尚未初始化”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15852958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"executenonquery connection property has not been initialized"
提问by Mirek
SqlConnection cn = new SqlConnection(@"DataSource=dbedu.cs.vsb.cz\SQLDB;Persist Security Info=True;User ID=*****;Password=*******");
SqlCommand cmd = new SqlCommand();
string finish = DropDownListFi.SelectedValue;
cn.Open();
String Name = Request.QueryString["Name"];
cmd.CommandText = "UPDATE navaznost_ukolu SET finish=@finish where Name='" + Name + "'";
cmd.Parameters.Add(new SqlParameter("@finish", finish));
cmd.ExecuteNonQuery();
cmd.Clone();
The error message
错误信息
Executenonquery connection property has not been initialized.
Executenonquery 连接属性尚未初始化。
采纳答案by John Woo
the problem with your current code is that you have not set the Connection
property of the SqlCommand
object. Try this,
您当前代码的问题在于您尚未设置对象的Connection
属性SqlCommand
。尝试这个,
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
and you must also parameterized the values set on the name
并且您还必须参数化设置在 name
String Name = Request.QueryString["Name"];
cmd.CommandText = "UPDATE navaznost_ukolu SET finish=@finish where Name=@name";
cmd.Parameters.Add(new SqlParameter("@finish", finish));
cmd.Parameters.Add(new SqlParameter("@name", Name));
FULL CODE
完整代码
string finish = DropDownListFi.SelectedValue;
String Name = Request.QueryString["Name"];
string connStr = @"DataSource=dbedu.cs.vsb.cz\SQLDB;
Persist Security Info=True;
User ID=*****;
Password=*******";
string sqlStatement = @"UPDATE navaznost_ukolu
SET finish = @finish
WHERE Name = @Name";
using (SqlConnection conn = new SqlConnection(connStr))
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = sqlStatement;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@finish", finish));
cmd.Parameters.Add(new SqlParameter("@name", Name));
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(SqlException e)
{
// do something with the exception
// do not hide it
// e.Message.ToString()
}
}
}
For proper coding
为了正确编码
- use
using
statement for propr object disposal - use
try-catch
block to properly handle objects
using
用于 propr 对象处理的use语句- 使用
try-catch
块来正确处理对象
回答by Tim Schmelter
The error is self-explanatory, you have not assigned the connection to the command. You can use the constructor:
该错误不言自明,您尚未将连接分配给命令。您可以使用构造函数:
using(var cn = new SqlConnection(@"DataSource=dbedu.cs.vsb.cz\SQLDB;Persist Security Info=True;User ID=*****;Password=*******"))
using(var cmd = new SqlCommand(
"UPDATE navaznost_ukolu SET finish=@finish where Name=@Name"
, cn))
{
string finish = DropDownListFi.SelectedValue;
cn.Open();
String Name = Request.QueryString["Name"];
cmd.Parameters.AddWithValue("@finish", finish);
cmd.Parameters.AddWithValue("@Name", Name);
cmd.ExecuteNonQuery();
}
Note that i've also used a sql-parameter for the Name
and using
statements to ensure that anything implementing IDisposable
gets disposed, even in case of an exception. This will also close the connection.
请注意,我还为Name
andusing
语句使用了一个 sql 参数,以确保任何实现IDisposable
都得到处理,即使在出现异常的情况下也是如此。这也将关闭连接。