求解,昨天在网上找的事务例子,照着做了根本没用啊.求好心人教教吧
using (SqlTransaction trans = objcon.BeginTransaction())
{
try
{
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
itmp++;
tmp = "insert into TEimpdatas Values('" + this.label3.Text + "','"
+ this.dataGridView1.Rows[i].Cells[0].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[1].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[2].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[3].Value.ToString() + "','"
+ DateTime.Now.ToLocalTime().ToString() + "','')";
if (itmp == 500)
{
itmp = 0;
SqlCommand objcom2 = new SqlCommand(tmp, objcon, trans);
objcom2.ExecuteNonQuery();
}
SqlCommand objcom = new SqlCommand(tmp, objcon, trans);
objcom.ExecuteNonQuery();
}
trans.Commit();
MessageBox.Show("OK!"); }
catch (Exception)
{
trans.Rollback();
//MessageBox.Show(ex.ToString());
}
finally
{
objcon.Close();
}
using (SqlTransaction trans = objcon.BeginTransaction())
{
try
{
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
itmp++;
tmp = "insert into TEimpdatas Values('" + this.label3.Text + "','"
+ this.dataGridView1.Rows[i].Cells[0].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[1].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[2].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[3].Value.ToString() + "','"
+ DateTime.Now.ToLocalTime().ToString() + "','')";
if (itmp == 500)
{
itmp = 0;
SqlCommand objcom2 = new SqlCommand(tmp, objcon, trans);
objcom2.ExecuteNonQuery();
}
SqlCommand objcom = new SqlCommand(tmp, objcon, trans);
objcom.ExecuteNonQuery();
}
trans.Commit();
MessageBox.Show("OK!"); }
catch (Exception)
{
trans.Rollback();
//MessageBox.Show(ex.ToString());
}
finally
{
objcon.Close();
}
解决方案 »
- 求教String.Format时有多个数值时该怎么写?
- vs2005配置要求
- 懒人高分求动态调用WebService类
- GridView绑定数据源
- 在线等~~~~~~~~页面传值问题
- 在WINFROM中,Datagrid绑定了一个数据集,数据集中有一字段为”AllowUsed“是字符类型,值是"1","0", 我想将AllowUsed字段在绑定时转换成
- 模拟键盘硬件输入汉字的问题
- 请各位大师帮忙解决一下
- 在网页中,点击一个按钮, 怎么触发另一个按钮的被单击的事件?
- C#如何通过点击按钮创建文件然后建立一个button代替那个文件
- VS2010编译时报错:'SelectByName' is not supported by language
- C# 中Dataset获取数据库数据计算。。。大神们!求解!
求指导优化这sql语句.. 好心人有好报哟
你直接拼接一个SQL 然后执行用事务执行就可以了。第三:
例如:StringBuiler sb = new StringBuilder();for(int ....................)
{
sb.Append(你的SQL )
}最后你在 BeginTransaction()
cmd.ExecuteNonQuery(); 然后 Commint();catch
{
RollBack();
}
if (itmp == 500) { itmp = 0; SqlCommand objcom2 = new SqlCommand(tmp, objcon, trans); objcom2.ExecuteNonQuery(); }用StringBuiler 保存,一次性提交?