2013-04-25 47 views
0

我想從一個文本框添加值到一個DataGridView,我已經問過這個問題,但現在我得到一個不同的錯誤說添加文本框的值在C#中的SQL數據庫

有INSERT語句中的列數多於VALUES子句中指定的數值。 VALUES子句中的值數量必須與INSERT語句中指定的列數相匹配。

這是導致錯誤

private void SaveBtn_Click(object sender, EventArgs e) 
{ 
    SqlConnection sc = new SqlConnection(); 
    SqlCommand com = new SqlCommand(); 
    sc.ConnectionString = ("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True"); 
    sc.Open(); 
    com.Connection = sc; 
    com.CommandText = ("INSERT INTO Stock (Prod_ID, Prod_Name, Prod_Cat, Supplier, Cost, Price_1, Price_2, Price_3) VALUES ('"+ProdID.Text+"''"+ProdName.Text+"'+'"+ProdCat.Text+"'+'"+ProdSup.Text+"'+'"+ProdCost.Text+"'+'"+ProdPrice1.Text+"'+'"+ProdPrice2.Text+"'+'"+ProdPrice3.Text+"');"); 
    com.ExecuteNonQuery(); 
    sc.Close(); 
} 
+3

您的價值數據字段之間沒有逗號,也請參數化! http://stackoverflow.com/questions/11905185/executing-query-with-parameters – 2013-04-25 13:26:40

+2

使用參數! – 2013-04-25 13:28:31

回答

3

你缺少逗號在SQL中的值部分的代碼。當你做這樣的事情時(字符串的大串聯),你應該知道兩件事情。首先,測試的一個好方法是寫出控制檯,messagebox,分機。你經常會馬上看到錯誤。接下來要知道的是,如果你想要插入一個數據庫,不要這麼做。使用參數化查詢。 - >How do parameterized queries help against SQL injection?

com.CommandText = ("INSERT INTO Stock (Prod_ID, Prod_Name, Prod_Cat, Supplier, Cost, Price_1, Price_2, Price_3) VALUES ('"+ProdID.Text+"''"+ProdName.Text+"'+'"+ProdCat.Text+"'+'"+ProdSup.Text+"'+'"+ProdCost.Text+"'+'"+ProdPrice1.Text+"'+'"+ProdPrice2.Text+"'+'"+ProdPrice3.Text+"');"); 

應該是這樣的

com.CommandText = (@"INSERT INTO Stock (Prod_ID, Prod_Name, Prod_Cat, Supplier, Cost, Price_1, Price_2, Price_3) VALUES ('"+ProdID.Text+"','"+ProdName.Text+"','"+ProdCat.Text+"','"+ProdSup.Text+"','"+ProdCost.Text+"','"+ProdPrice1.Text+"','"+ProdPrice2.Text+"','"+ProdPrice3.Text+"');")); 
+0

它現在正在工作!非常感謝= D – 2013-04-25 13:33:09

+1

@ReeceCottam請確保您閱讀了我最後一次關於SQL注入的編輯。從用戶處獲取輸入並使用串聯構建查詢非常困難。非常非常糟糕。我的意思是你能做的最糟糕的事情。 – 2013-04-25 13:34:50

+0

@ReeceCottam如果我的答案解決了您的問題,請接受。 – 2013-04-25 13:46:16

0

嘗試:

com.CommandText = ("INSERT INTO Stock (Prod_ID, Prod_Name, Prod_Cat, Supplier, Cost, Price_1, Price_2, Price_3) VALUES ('"+ProdID.Text+"','"+ProdName.Text+"','"+ProdCat.Text+"','"+ProdSup.Text+"','"+ProdCost.Text+"','"+ProdPrice1.Text+"','"+ProdPrice2.Text+"','"+ProdPrice3.Text+"');"); 
+0

這是一個選項,但它很容易受到SQL注入的影響。使用準備好的語句更好。 – Stefan 2013-04-26 13:29:04

+0

@StefanM但這是他想要的! – Obama 2013-05-01 14:23:56

-1

就應該更換

('"+ProdID.Text+"''"+ProdName.Text+"'+'"+ProdCat.Text+"'+'"+ProdSup.Text+"'+'"+ProdCost.Text+"'+'"+ProdPrice1.Text+"'+'"+ProdPrice2.Text+"'+'"+ProdPrice3.Text+"');");` 

('"+ProdID.Text+"','"+ProdName.Text+"','"+ProdCat.Text+"','"+ProdSup.Text+"','"+ProdCost.Text+"','"+ProdPrice1.Text+"','"+ProdPrice2.Text+"','"+ProdPrice3.Text+"');");` 

VALUES部分需要爲每一列逗號)

+0

請解釋downvote。 – 2013-04-25 14:39:49

1

複選框的值的形式或者導致毫無關係,如果沒有複選框被選中,或逗號delimted值列表。你可能做的最糟糕的事情就是將這個列表存儲在單個記錄中。這會導致無法使用的數據。

相反,您不僅要更改您的代碼,還要更改您的數據庫設計,以便每個檢查過的盒子都有一個記錄。請記住考慮沒有選中框的場景。

+0

+1,這裏有很多問題。 SQL注入問題,來自ui的數據庫訪問,不使用語句。希望這只是一個測試/學習應用程序。 – 2013-04-25 13:43:03

3

錯誤的直接原因在查詢的「值」部分省略逗號(「,」)。 你就應該把它像

VALUES ('"+ProdID.Text+"', '"+ProdName.Text+", '+'"+ProdCat.Text+", '+'"+ProdSup.Text+... 

,而不是

VALUES ('"+ProdID.Text+"''"+ProdName.Text+"'+'"+ProdCat.Text+"'+'"+ProdSup.Text+... 

您的代碼也容易受到所謂的SQL注入攻擊(想象一下,有人已經把 「」從庫存中刪除 - 」到ProdID.Text:在execution'll結果庫存表 間隙)

推薦的方法看起來是這樣的:

using(SqlConnection sc = new SqlConnection()) { 
    sc.ConnectionString = "Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True"; 
    sc.Open(); 

    using (SqlCommand com = sc.CreateCommand()) { 
    com.CommandText = 
     "insert into Stock(\n" + 
     " Prod_Id,\n" + 
     " Prod_Name,\n" + 
     " Prod_Cat,\n" + 
     " Supplier,\n" + 
     " Cost,\n" + 
     " Price_1,\n" + 
     " Price_2,\n" + 
     " Price_3)\n" + 
     "values(\n" + 
     " @prm_Prod_Id,\n" + 
     " @prm_Prod_Name,\n" + 
     " @prm_Prod_Cat,\n" + 
     " @prm_Supplier,\n" + 
     " @prm_Cost,\n" + 
     " @prm_Price_1,\n" + 
     " @prm_Price_2,\n" + 
     " @prm_Price_3)"; 

    //TODO: Change my arbitrary "80" to actual Stock fields' sizes! 
    com.Parameters.Add("@prm_Prod_Id", SqlDbType.VarChar, 80).Value = ProdID.Text; 
    com.Parameters.Add("@prm_Prod_Name", SqlDbType.VarChar, 80).Value = ProdName.Text; 
    com.Parameters.Add("@prm_Prod_Cat", SqlDbType.VarChar, 80).Value = ProdCat.Text; 
    com.Parameters.Add("@prm_Supplier", SqlDbType.VarChar, 80).Value = ProdSub.Text; 
    com.Parameters.Add("@prm_Cost", SqlDbType.VarChar, 80).Value = ProdCost.Text; 
    com.Parameters.Add("@prm_Price_1", SqlDbType.VarChar, 80).Value = ProdPrice1.Text; 
    com.Parameters.Add("@prm_Price_2", SqlDbType.VarChar, 80).Value = ProdPrice2.Text; 
    com.Parameters.Add("@prm_Price_3", SqlDbType.VarChar, 80).Value = ProdPrice3.Text; 

    com.ExecuteNonQuery(); 
    } 
} 
相關問題