2013-06-03 31 views
2

我正在建立一個數據庫使用Visual Studio 2008年c#,當我試圖插入一個新的記錄到我的數據庫看起來ExecuteNonQuery尚未初始化。我複製我的代碼,希望任何人都可以幫助我,因爲我是新手。ExecuteNonQuery不工作在C#

private void button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Usuario\Documents\Visual Studio 2010\Projects\Nova\Nova\Database1.mdf;Integrated Security=True;User Instance=True"); 
    SqlCommand cmd = new SqlCommand(); 
    cn.Open(); 
    cmd.CommandText = "insert into Database1.mdf(Codigo,Nombre,Cantidad,Tipo) values('"+comboBox1.Text+"','"+textBox3.Text+"','"+textBox1.Text+"','"+comboBox2.Text+"')"; 
    cmd.ExecuteNonQuery(); 
    cmd.Clone(); 
    cn.Close(); 
    MessageBox.Show("Acabas de agregar un producto"); 
} 
+1

你會得到例外嗎?與我們分享? – Mzf

+0

您必須爲命令對象分配連接。 –

+6

您的代碼易受SQL注入攻擊。另外,你應該使用'using'塊來實現IDisposable接口,特別是SqlConnection類,以便在發生異常時關閉連接。 –

回答

13

您還沒有設置您的命令連接:

cmd.Connection = cn; 
+0

這也讓我很開心:(我希望它會拋出一個錯誤 – heymega

1

你沒有用連接初始化SqlCommand。另外,你應該在這裏附上一切與using。並考慮使用參數化命令來避免SQL注入。

private void button1_Click(object sender, EventArgs e) 
    { 
     using (SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Usuario\Documents\Visual Studio 2010\Projects\Nova\Nova\Database1.mdf;Integrated Security=True;User Instance=True")) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.CommandText = "insert into databaseTableName (Codigo,Nombre,Cantidad,Tipo) values (@Codigo, @Nombre, @Cantidad, @Tipo)"; 
       cmd.Parameters.AddWithValue("@Codigo", comboBox1.Text); 
       cmd.Parameters.AddWithValue("@Nombre", textBox3.Text); 
       cmd.Parameters.AddWithValue("@Cantidad", textBox1.Text); 
       cmd.Parameters.AddWithValue("@Tipo", comboBox2.Text); 
       cmd.Connection = cn; //this was where the error originated in the first place. 
       cn.Open(); 
       cmd.ExecuteNonQuery(); 
       MessageBox.Show("Acabas de agregar un producto"); 
      } 
     } 
    } 
+0

+1好,但不需要關閉連接 – Steve

+0

真相!編輯出來,是一個疏忽,因爲我試圖毆打你的答案;) –

8

你在你的代碼的許多問題:

  • 第一:insert into statement需要一個目標數據表中沒有的 名MDF文件
  • 二:採用using statement關閉和處置連接
  • 第三:Parametrized query爲了避免解析問題和sql 注射
  • 第四:你需要到命令連接關聯(輕鬆 在SqlCommand constructor完成)

    using(SqlConnection cn = new SqlConnection(.......)) 
    using(SqlCommand cmd = new SqlCommand("insert into table_name(Codigo,Nombre,Cantidad,Tipo)" + 
              "values (@cod, @nom,@can,@tipo)", con)) 
    { 
        cn.Open(); 
        cmd.Parameters.AddWithValue("@cod", comboBox1.Text); 
        cmd.Parameters.AddWithValue("@nom", textBox3.Text); 
        cmd.Parameters.AddWithValue("@can", textBox1.Text); 
        cmd.Parameters.AddWithValue("@tipo", comboBox2.Text); 
        cmd.ExecuteNonQuery(); 
        MessageBox.Show("Acabas de agregar un producto"); 
    } 
    

編輯 下面由@RemusRusanu添加的鏈接提供的信息是非常重要的。使用AddWithValue雖然方便,但可能會妨礙查詢的性能。正確的方法應該是使用具有顯式數據類型和參數大小的正確定義的SqlParameter。 作爲一個例子

SqlParameter p = new SqlParameter("@cod", SqlDbType.NVarChar, 255).Value = comboBox1.Text; 
cmd.Parameters.Add(p); 

但是,當然,這需要你檢查你列的確切數據類型和大小。

+0

在第一個'{'之前,你有一個額外的分號。另外,即使你在第一個'using'語句之後不需要*大括號,它們會使查看範圍更容易。 – egrunin

+0

是的,複製粘貼總是讓我失敗 – Steve

+0

我真的不贊同大括號,雖然 – Steve