在for循环中运行插件更新等SQL查询是不错的主意? [英] Is it bad idea to run SQL queries like insert update in a for loop?
本文介绍了在for循环中运行插件更新等SQL查询是不错的主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if ( value > 0 )
{
for (< span class =code-keyword> int i = 0 ; i< mydatagrid.Rows.Count; i ++)
{
SqlCommand cc = new SqlCommand( 插入商店值(@ sbno,@ vno,@ box,@ qty,@ pcs,@ remark,@ avail),cn);
cc.Parameters.AddWithValue( @ sbno,sbno);
cc.Parameters.AddWithValue( @ vno,vno);
cc.Parameters.AddWithValue( @ box, Int32 .Parse(dataGridView1.Rows [i] .Cells [ 0 ]。Value.ToString()));
cc.Parameters.AddWithValue( @ qty, Int32 .Parse(dataGridView1.Rows [i] .Cells [ 1 ]。Value.ToString()));
cc.Parameters.AddWithValue( @ pcs,);
cc.Parameters.AddWithValue( @ remark,);
cc.Parameters.AddWithValue( @ avail,);
cc.ExecuteNonQuery();
}
MessageBox.Show(nstdbox + 存储的存储箱) ;
}
我尝试过:
是坏还是好?我不知道关于这个的做法
我做错了吗?
解决方案
如何使用存储过程并传递一个DataTable(网格行)作为参数并在存储过程中执行Insert / Update?这是一个可能有用的例子。
将DataTable发送到存储过程 [ ^ ]
基本技术是:
设置命令
添加参数 - 不要使用AddWithValue
迭代行
设置参数值
执行命令
并且不执行以下操作:Int32.Parse( dataGridView1.Rows [I] .Cells [0] .Value.ToString())
if (value > 0)
{
for(int i=0;i<mydatagrid.Rows.Count;i++)
{
SqlCommand cc = new SqlCommand("insert into store values(@sbno,@vno,@box,@qty,@pcs,@remark,@avail)", cn);
cc.Parameters.AddWithValue("@sbno", sbno);
cc.Parameters.AddWithValue("@vno", vno);
cc.Parameters.AddWithValue("@box",Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()));
cc.Parameters.AddWithValue("@qty",Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString()));
cc.Parameters.AddWithValue("@pcs",);
cc.Parameters.AddWithValue("@remark",);
cc.Parameters.AddWithValue("@avail", );
cc.ExecuteNonQuery();
}
MessageBox.Show(nstdbox + " Boxes Stored To Store");
}
What I have tried:
it is bad or ok? I don't know the practice about this
Am i doing it wrong way?
解决方案
How about using a Stored Procedure and pass a DataTable (Grid rows) as parameter and do the Insert/Update in the stored procedure? Here is an example that could be useful.
Sending a DataTable to a Stored Procedure[^]
The basic technique for this is:
Set up the command Add the parameters -- do not use AddWithValue Iterate the rows Set the parameter values Execute the command
And do not do anything like:Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString())
这篇关于在for循环中运行插件更新等SQL查询是不错的主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文