C# ExecuteReader:尚未初始化连接属性
ExecuteReader:连接属性已更改 尚未初始化 我的编码是C# ExecuteReader:尚未初始化连接属性,c#,asp.net,sql-server-2005,C#,Asp.net,Sql Server 2005,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 =
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();
}
}
}
}
}
必须将连接指定给命令对象,如
SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')");
cmd.Connection = conn;
使用此和传递连接对象:
SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')",conn);
在
SqlCommand cmd=newsqlcommand(“插入到时间(项目、迭代)值('..
加
希望此帮助如前所述,您应该分配连接,最好也使用sql参数,这样您的命令分配将为:
// 3. Pass the connection to a command object
SqlCommand cmd=new SqlCommand ("insert into time(project,iteration) values (@project, @iteration)", conn); // ", conn)" added
cmd.Parameters.Add("project", System.Data.SqlDbType.NVarChar).Value = this.name1.SelectedValue;
cmd.Parameters.Add("iteration", System.Data.SqlDbType.NVarChar).Value = this.name1.SelectedValue;
//
// 4. Use the connection
//
通过使用参数,您可以避免SQL注入和其他有问题的输入错误(例如项目名称“myproject”就是一个例子)。所有答案都是正确的。这是另一种方式。我喜欢这种方式
SqlCommand cmd = conn.CreateCommand()
您必须注意到字符串concat存在sql注入问题。
使用参数
您也可以这样写:
SqlCommand cmd=new SqlCommand ("insert into time(project,iteration) values (@project, @iteration)", conn);
cmd.Parameters.AddWithValue("@project",name1.SelectedValue);
cmd.Parameters.AddWithValue("@iteration",iteration.SelectedValue);
我喜欢使用语句将我所有的sql连接放在
中。我认为它们看起来更干净,当您完成它们时,它们会自行清理。我还建议对每个查询进行参数化,这样不仅更安全,而且如果您需要返回并进行更改,则更易于维护
// create/open connection
using (SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI")
{
try
{
conn.Open();
// initialize command
using (SqlCommand cmd = conn.CreateCommand())
{
// generate query with parameters
with cmd
{
.CommandType = CommandType.Text;
.CommandText = "insert into time(project,iteration) values(@name, @iteration)";
.Parameters.Add(new SqlParameter("@name", this.name1.SelectedValue));
.Parameters.Add(new SqlParameter("@iteration", this.iteration.SelectedValue));
.ExecuteNonQuery();
}
}
}
catch (Exception)
{
//throw;
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close;
}
}
}
由于SqlConnection、SqlCommand和SqlReader对象使用的是非托管资源,因此它们是一次性对象,因此在完成任务时处理它们是一种很好的做法。为了使代码更具可读性,您可以使用using指令来执行此操作。这些答案是正确的。您必须接受。必须使用初始化SqlCommand connection属性您创建的连接。您好,谢谢您提供的信息。我还有另一个说明,如何在c#中获取html控件?什么是“oCn”,为什么有一个“with”或它在做什么?with“是我太懒了,没有输入cmd。对于接下来的五个命令,oCn是一个输入错误……这是我工作中连接对象的命名约定。谢谢。
// create/open connection
using (SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI")
{
try
{
conn.Open();
// initialize command
using (SqlCommand cmd = conn.CreateCommand())
{
// generate query with parameters
with cmd
{
.CommandType = CommandType.Text;
.CommandText = "insert into time(project,iteration) values(@name, @iteration)";
.Parameters.Add(new SqlParameter("@name", this.name1.SelectedValue));
.Parameters.Add(new SqlParameter("@iteration", this.iteration.SelectedValue));
.ExecuteNonQuery();
}
}
}
catch (Exception)
{
//throw;
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close;
}
}
}