2011-05-03 244 views
31

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(); 
     } 
    } 
    } 
    } 

    } 
+0

由于SqlConnection,SqlCommand和SqlReader对象正在使用非托管资源,因此它们是一次性对象,因此在任务完成时处理它们是一种很好的做法。为了使代码更具可读性,您可以使用using指令来执行此操作。 – Beatles1692 2011-05-03 06:56:20

+0

这些答案是正确的。您必须接受。您必须使用您创建的连接初始化sqlcommand连接属性。 – Saleh 2011-05-03 06:57:53

回答

54

使用此,并通过连接对象:

SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')",conn); 
+0

嗨,感谢您的信息..我也有另一个澄清如何得到C#中的HTML控件# – jeni 2011-05-03 07:05:43

+0

如果它是一条蛇...... /叹 – ruffin 2016-02-23 16:41:45

6

你必须指定连接到你的命令对象,如..

SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')"); 
cmd.Connection = conn; 
14

经过SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('.... 添加

cmd.Connection = conn; 

希望这有助于

2

如前所述,你应该分配连接,你最好也应使用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's”这样的项目名称就是一个例子)。

3

你也可以这样写:

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); 
1

我喜欢放在using声明我所有的SQL连接。我认为他们看起来更干净,当他们完成之后他们会自己清理干净。我还建议参数化每个查询,不仅更安全,而且如果需要返回并进行更改,则更容易维护。

// create/open connection 
using (SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI") 
{ 
    try 
    { 
     oCn.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; 
     } 
    } 
} 
相关问题