在数据库中插入数据 [英] to insert data in database
问题描述
cmd.Parameters.Add( new SqlParameter( < span class =code-string> @ itemno,SqlDbType.Int));
cmd.Parameters.Add( new SqlParameter( @itemname,SqlDbType.VarChar));
cmd.Parameters.Add( new SqlParameter( @price,SqlDbType.Decimal));
cmd.Parameters.Add( new SqlParameter( @quantity,SqlDbType.Decimal));
cmd.Parameters.Add( new SqlParameter( @weight,SqlDbType.Decimal));
cmd.Parameters.Add( new SqlParameter( @itemtotal,SqlDbType.Decimal));
con.Open ();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
cmd.Parameters [ < span class =code-string> @ itemno]。值= row.Cells [ 0 ]。值;
cmd.Parameters [ @ itemname]。值= row.Cells [ 1 跨度>]值。
cmd.Parameters [ @ price]。值= row.Cells [ 2 跨度>]值。
cmd.Parameters [ @ quantity]。值= row.Cells [ 3 跨度>]值。
cmd.Parameters [ @ weight]。值= row.Cells [ 4 跨度>]值。
cmd.Parameters [ @ itemtotal]。值= row.Cells [ 5 跨度>]值。
}
}
cmd.ExecuteNonQuery();
MessageBox.Show( Procedure Executed);
}
catch (Exception ex)
{MessageBox.Show(ex.Message); }
finally
{con.Close(); }
}
我的最后一行数据在数据库中插入两次....
我怎么能解决这个问题
我试过用cmd.ExecuteNonQuery();在循环之外,但是我在sql数据库中的最后一行数据是在数据库中插入两次(重复)..........
plz建议写代码纠正错误........在此代码
您的问题是因为您正在调用cmd.ExecuteNonQuery() ;在循环内部然后一次在外面。
如果从MessageBox.Show行之前删除该行,那么它应该解决你的问题问题。
使用事务而不是单次提交也可以解决你的问题而且性能降低更少
只需写一下这个:
con.Open();
// 初始化交易
private SqlTransaction trans = con.BeginTransaction();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
cmd.Parameters [ @ itemno]。值= row.Cells [ 0 ]。值;
cmd.Parameters [ @ itemname]。值= row.Cells [ 1 跨度>]值。
cmd.Parameters [ @ price]。值= row.Cells [ 2 跨度>]值。
cmd.Parameters [ @ quantity]。值= row.Cells [ 3 跨度>]值。
cmd.Parameters [ @ weight]。值= row.Cells [ 4 跨度>]值。
cmd.Parameters [ @ itemtotal]。值= row.Cells [ 5 跨度>]值。
// 向交易添加命令并执行它
cmd。交易=反转;
cmd.ExecuteNonQuery();
}
}
MessageBox.Show( 程序执行);
尝试
{
// 提交事务(所有命令)
此 .trans.Commit();
this .trans.Dispose();
此 .trans = null ;
}
catch ()
{
}
finally
{
if (trans!= null )
{
trans.Dispose();
trans = null ;
}
}
}
catch (Exception ex)
{MessageBox.Show(ex.Message) ); }
finally
{con.Close(); }
}
如果我没有犯错我希望如此,cmd存储在交易并在trans.Commit()之后提交,所以你的命令按正确顺序完成,但是一步完成。
没有双重插入,如果出现问题,只是Transaction.Rollback();
并且不会做任何更改,就像我说的缓冲区一样。
:)
cmd.Parameters.Add(new SqlParameter("@itemno", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@itemname", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("@price", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@quantity", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@weight", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@itemtotal", SqlDbType.Decimal));
con.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
cmd.Parameters["@itemno"].Value = row.Cells[0].Value;
cmd.Parameters["@itemname"].Value = row.Cells[1].Value;
cmd.Parameters["@price"].Value = row.Cells[2].Value;
cmd.Parameters["@quantity"].Value = row.Cells[3].Value;
cmd.Parameters["@weight"].Value = row.Cells[4].Value;
cmd.Parameters["@itemtotal"].Value = row.Cells[5].Value;
}
}
cmd.ExecuteNonQuery();
MessageBox.Show("Procedure Executed");
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{ con.Close(); }
}
My last row data is inserting twice in database ....
how can i solve this problem
I have tried t use cmd.ExecuteNonQuery(); outside of loop , but still my last row data in sql database is inserting twice (repeatedly ) in database..........
plz suggest write code to correct error........in this code
Your problem is because you are callingcmd.ExecuteNonQuery();inside the loop and then once outside.
If you remove that line from just before the MessageBox.Show line then it should solve your issue.
using a Transaction instead of single commits might also solve your Problem and is less Performance killing
just write it like this:
con.Open(); //initializing transaction private SqlTransaction trans = con.BeginTransaction(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { cmd.Parameters["@itemno"].Value = row.Cells[0].Value; cmd.Parameters["@itemname"].Value = row.Cells[1].Value; cmd.Parameters["@price"].Value = row.Cells[2].Value; cmd.Parameters["@quantity"].Value = row.Cells[3].Value; cmd.Parameters["@weight"].Value = row.Cells[4].Value; cmd.Parameters["@itemtotal"].Value = row.Cells[5].Value; //adding command to the Transaction and "executing" it cmd.Transaction = trans; cmd.ExecuteNonQuery(); } } MessageBox.Show("Procedure Executed"); try { //commit the Transaction ( all commands) this.trans.Commit(); this.trans.Dispose(); this.trans = null; } catch () { } finally { if (trans != null) { trans.Dispose(); trans = null; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } }
If i made no mistakes "i hope so", the cmd is stored into the Transaction and is commited after trans.Commit(), so your commands get done in correct order but in "one step".
No double inserts and if something goes wrong, just Transaction.Rollback();
and no changes will be done, ist like a buffer i'd say.
:)
这篇关于在数据库中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!