2011-04-06 158 views
0

我正在創建一個基於Windows的應用程序來讀取csv文件並在datagridview中顯示。 csv文件由item_no和stock組成。從csv文件替換數據庫中的數據

我有兩個按鈕,添加數據和替換數據。

與地址數據,同樣ITEM_NO庫存得到補充,然後將其插入到數據庫中。在數據庫中,庫存添加了特定item_no的現有庫存。

隨着替代數據,同樣ITEM_NO庫存得到補充,並隨後與ITEM_NO的數據庫中的股票替代。

我能夠加入股票數據庫,但我不能代替股票在數據庫中。

這裏是我的代碼來替換數據,更可能同加高湯,

try 
    { 

    //we need to copy the data from datagridview into data table 
    DataTable dtable = new DataTable(); 

    dtable.TableName = "Product"; 

    foreach (DataGridViewColumn col in dataGridView1.Columns) 
    { 
     dtable.Columns.Add(col.DataPropertyName, col.ValueType); 
    } 

      foreach (DataGridViewRow row in dataGridView1.Rows) 

      { 

       if (row.IsNewRow) 

        continue; 



       DataRow dtRow = dtable.NewRow(); 



       for (int i = 0; i < dataGridView1.Columns.Count; i++) 

        dtRow[i] = (row.Cells[i].Value == null ? DBNull.Value : row.Cells[i].Value); 



       dtable.Rows.Add(dtRow); 

      } 



      foreach (DataRow dr in dtable.Rows) 

      { 

       string itemNo = null; 

       string itemName = null; 

       double cost = 0.00; 

       double price = 0.00; 

       double Stock = 0.00; 

       int dept = 1; 

       double tax1 = 0; 

       double tax2 = 0; 

       double BulkPrize = 0.00; 

       double BulkQty = 0.00; 

      // double finalStock = 0.00; 

       itemNo = Convert.ToString(dr[0]); 



       if (dr[1] != DBNull.Value) 

        itemName = Convert.ToString(dr[1]); 





       if (dr[2] != DBNull.Value) 

        price = Convert.ToDouble(dr[2]); 





       if (dr[3] != DBNull.Value) 

        cost = Convert.ToDouble(dr[3]); 



       Stock = Convert.ToDouble(dr[4]); 

       // finalStock = finalStock + Stock; 



       if (dr[5] != DBNull.Value) 

        dept = Convert.ToInt32(dr[5]); 



       if (dr[6] != DBNull.Value) 

        tax1 = Convert.ToDouble(dr[6]); 



       if (dr[7] != DBNull.Value) 

        tax2 = Convert.ToDouble(dr[7]); 



       string sql_select = "select count(*) from PRODUCTS where item_no= '" + itemNo + "'"; 

       SqlCommand cmdCheckPmk = new SqlCommand(sql_select, Class1.conn); 



       int selectItemNo = Convert.ToInt32(cmdCheckPmk.ExecuteScalar()); 



       if (selectItemNo != 0) 

       { 

        string sql_update = "update PRODUCTS set item_stock=+'" + Stock + "' where item_no= '" + itemNo + "'"; 

        SqlCommand cmd1 = new SqlCommand(sql_update, Class1.conn); 

        cmd1.ExecuteNonQuery(); 



       } 

       else 

       { 

        SqlCommand cmd11 = new SqlCommand("insert into PRODUCTS(item_no,item_name,price,cost,item_stock,dept_id,tax_rate1,tax_rate2,bulk_price,bulk_qty) values ('" + itemNo + "','" + itemName + "'," + price + "," + cost + "," + Stock + ",'" + dept + "','" + tax1 + "','" + tax2 + "'," + BulkPrize +"," + BulkQty +") ", Class1.conn); 

        cmd11.ExecuteNonQuery(); 

       } 

      } 



      MessageBox.Show("Data Replace Successfully ..!!", "Congrats", MessageBoxButtons.OK, MessageBoxIcon.Information); 

     } 

     catch (System.Data.SqlClient.SqlException exe) 

     { 

      if (exe.Number == 547) 

      { 

       MessageBox.Show("Add Department in Corner Store First..!!", "Add Department in Corner Store", MessageBoxButtons.OK, MessageBoxIcon.Error); 

      } 

      else 

      { 

       MessageBox.Show(exe.Message, "Error in replace - Option 3 Insert/Update", MessageBoxButtons.OK, MessageBoxIcon.Error); 

      } 

     } 

    } 

誰能幫助我?

謝謝。

Rushabh

+4

我看到其他人在您之前的一些問題中提到了這一點,但我會再次提及它。如果你接受了一些答案,你可能會得到更多的幫助。如果有人提供了有用的答案,請點擊旁邊的複選框。 – 2011-04-06 21:55:18

+3

你知道如何接受答案嗎?你仍然沒有接受你今天早些時候發佈的http://stackoverflow.com/questions/5570172/retrieve-value-from-csv-file-and-replace-it-in-database-in-c的答案。在某些時候,人們將不再回答你的問題。 – StriplingWarrior 2011-04-06 21:57:03

回答

0

這看起來不正確

string sql_update = "update PRODUCTS set item_stock=+'" + Stock + "' where item_no= '" + itemNo + "'"; 

我想你想(試試這個

string sql_update = "update PRODUCTS set item_stock='" + Stock + "' where item_no= '" + itemNo + "'"; 

而且我不知道,如果你的ITEM_NO是int或不是,如果它是一個int您需要

string sql_update = "update PRODUCTS set item_stock='" + Stock + "' where item_no = " + itemNo ; 
+0

item_no是字符串。我需要添加庫存,然後用數據庫中的值替換最終庫存值 – 2011-04-06 22:03:13

+0

您是否嘗試過我的更正版本? – inspite 2011-04-06 22:18:47

+0

我試過了,但沒有奏效。你能否給我一些其他的替代方案.... – 2011-04-07 03:02:37