从csv文件替换数据库中的数据
本文关键字:数据 数据库 替换 csv 文件 | 更新日期: 2023-09-27 17:48:56
我正在创建一个基于Windows的应用程序来读取csv文件并在datagridview中显示。csv文件由item_no和stock组成。
我有两个按钮,添加数据和替换数据。
使用add data,添加相同的item_no stock,然后将其插入数据库。在数据库中,库存被添加到特定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
这看起来不对
string sql_update = "update PRODUCTS set item_stock=+'" + Stock + "' where item_no= '" + itemNo + "'";
我想你要(try this)
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 ;