2011-05-03 245 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; 
     } 
    } 
} 
相關問題