我的数据集无法正常工作
My dataset doesn't work properly
我的问题是,当它更新时,它会一次又一次地添加以前的数据
我使用 telerik 网格视图
这里是我的 3 层代码
第一个
private void btnSbmt_Click(object sender, EventArgs e)
{
foreach (var row in radGridView1.Rows)
{
_MyName.Add((string)row.Cells[1].Value);
}
foreach (var row in radGridView1.Rows)
{ // 0 - first column
_MyAmount.Add((int)row.Cells[2].Value);
}
foreach (var row in radGridView1.Rows)
{
_MyPrice.Add((decimal)row.Cells[3].Value);
}
Ref_View_Model = new View_model._View_Model();
Ref_View_Model.GetInsertProduct(_myName, _myAmount, _myPrice, txtDt.Text);
radGridView1.CurrentRow.Delete();
productTableAdapter.Update(sales_and_Inventory_SystemDataSet);
productTableAdapter.Fill(sales_and_Inventory_SystemDataSet.Product);
MessageBox.Show("Product(s) were added", "Done", MessageBoxButtons.OK);}
第二个
public void GetInsertProduct( List<string> _name, List<int> _amount, List<decimal> _price, string _date)
{
Ref_Model = new Model._Model();
Ref_Model.InsertProduct( _name, _amount, _price, _date);
}
第三个
public void InsertProduct(List<string> _myName,
List<int> _myAmount,
List<decimal> _myPrice, string _date)
{
Connection_String = 我的连接字符串
Query = @"INSERT INTO dbo.product(Name, Amount, Price, [date])
VALUES(@Name, @Amount, @Price, @Date);";
using ( Con = new SqlConnection(Connection_String))
using ( Cmd = new SqlCommand(Query, Con))
{
Cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Amount", SqlDbType.Int);
Cmd.Parameters.Add("@Price", SqlDbType.Decimal);
// Cmd.Parameters.Add("@Date", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Convert.ToDateTime(_date);
Cmd.Connection = Con;
Con.Open();
int recordsToAdd = _myName.Count();
for(int x = 0; x < recordsToAdd; x++)
{
Cmd.Parameters["@Name"].Value = _myName[x];
Cmd.Parameters["@Amount"].Value = _myAmount[x];
Cmd.Parameters["@Price"].Value = _myPrice[x];
Cmd.Parameters["@Date"].Value = _date;
Cmd.ExecuteNonQuery();
}
}
}
您似乎在使用全局变量来保存您从网格中读取的值。如果您在第一次插入后没有清除它们,您仍然在全局列表中有值,然后将它们再次添加到数据表中
当然,您可以仅使用一个循环来重新加载具有网格中存在的实际值的全局变量
private void btnSbmt_Click(object sender, EventArgs e)
{
// This removes whatever is in the lists
_MyName.Clear();
_MyAmount.Clear();
_MyPrice.Clear();
// and now start adding items from scratch
foreach (var row in radGridView1.Rows)
{
_MyName.Add((string)row.Cells[1].Value);
_MyAmount.Add((int)row.Cells[2].Value);
_MyPrice.Add((decimal)row.Cells[3].Value);
}
....
我的问题是,当它更新时,它会一次又一次地添加以前的数据
我使用 telerik 网格视图
这里是我的 3 层代码
第一个
private void btnSbmt_Click(object sender, EventArgs e)
{
foreach (var row in radGridView1.Rows)
{
_MyName.Add((string)row.Cells[1].Value);
}
foreach (var row in radGridView1.Rows)
{ // 0 - first column
_MyAmount.Add((int)row.Cells[2].Value);
}
foreach (var row in radGridView1.Rows)
{
_MyPrice.Add((decimal)row.Cells[3].Value);
}
Ref_View_Model = new View_model._View_Model();
Ref_View_Model.GetInsertProduct(_myName, _myAmount, _myPrice, txtDt.Text);
radGridView1.CurrentRow.Delete();
productTableAdapter.Update(sales_and_Inventory_SystemDataSet);
productTableAdapter.Fill(sales_and_Inventory_SystemDataSet.Product);
MessageBox.Show("Product(s) were added", "Done", MessageBoxButtons.OK);}
第二个
public void GetInsertProduct( List<string> _name, List<int> _amount, List<decimal> _price, string _date)
{
Ref_Model = new Model._Model();
Ref_Model.InsertProduct( _name, _amount, _price, _date);
}
第三个
public void InsertProduct(List<string> _myName,
List<int> _myAmount,
List<decimal> _myPrice, string _date)
{ Connection_String = 我的连接字符串
Query = @"INSERT INTO dbo.product(Name, Amount, Price, [date])
VALUES(@Name, @Amount, @Price, @Date);";
using ( Con = new SqlConnection(Connection_String))
using ( Cmd = new SqlCommand(Query, Con))
{
Cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Amount", SqlDbType.Int);
Cmd.Parameters.Add("@Price", SqlDbType.Decimal);
// Cmd.Parameters.Add("@Date", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Convert.ToDateTime(_date);
Cmd.Connection = Con;
Con.Open();
int recordsToAdd = _myName.Count();
for(int x = 0; x < recordsToAdd; x++)
{
Cmd.Parameters["@Name"].Value = _myName[x];
Cmd.Parameters["@Amount"].Value = _myAmount[x];
Cmd.Parameters["@Price"].Value = _myPrice[x];
Cmd.Parameters["@Date"].Value = _date;
Cmd.ExecuteNonQuery();
}
}
}
您似乎在使用全局变量来保存您从网格中读取的值。如果您在第一次插入后没有清除它们,您仍然在全局列表中有值,然后将它们再次添加到数据表中
当然,您可以仅使用一个循环来重新加载具有网格中存在的实际值的全局变量
private void btnSbmt_Click(object sender, EventArgs e)
{
// This removes whatever is in the lists
_MyName.Clear();
_MyAmount.Clear();
_MyPrice.Clear();
// and now start adding items from scratch
foreach (var row in radGridView1.Rows)
{
_MyName.Add((string)row.Cells[1].Value);
_MyAmount.Add((int)row.Cells[2].Value);
_MyPrice.Add((decimal)row.Cells[3].Value);
}
....