C#事务的问题?

sdweihailh 2012-12-24 01:57:39
C#:
try
{
cmdInsert = new SqlCommand(strInsertSQL, conn);
trans = conn.BeginTransaction(); // 开始事务
cmdInsert.Transaction = trans; // 将事务与SqlCommand关联
for(int i=0; i<ds.Tables[0].Rows.Count; i++)
{
DataRow row = ds.Tables[0].Rows[i];
*************************************************
SqlParameter[] paramArray = getParamArray(row);
cmdInsert.Parameters.Clear();
for (int j=0; j<paramArray.Length; j++)
{
cmdInsert.Parameters.Add(paramArray[j]);
}
cmdInsert.ExecuteNonQuery();
}
trans.Commit(); // 提交事务
}
catch(Exception ex)
{
trans.Rollback();
}
在***的一行加 SQLDATAREADER 读取同一CONNECTION的数据库信息进行比较;执行错误;
不加****,执行没问题,
是不是开始事务后;连接就锁定了,一直到事务结束。(中间如果我还想用同一连接取数据库的信息,循环比较,怎么做啊?)
...全文
193 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdweihailh 2012-12-25
  • 打赏
  • 举报
回复
就是这段代码;提交一条数据没问题;提交两条以上回提示超时;不明白到底哪出现问题了
sdweihailh 2012-12-25
  • 打赏
  • 举报
回复
try { getcon(); tran = My_con.BeginTransaction(); SQLCOM = new SqlCommand(); SQLCOM.Connection = My_con; SQLCOM.Transaction = tran; for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++) { string sqlcode; string leiname = combo_invalei.Text; string invadh = text_invadh.Text; string lei = combo_invastyle.Text; string operater = text_zd.Text; string hh = this.dataGridView1.Rows[i].Cells[0].Value.ToString(); string item = this.dataGridView1.Rows[i].Cells[1].Value.ToString(); string itemdesc = this.dataGridView1.Rows[i].Cells[2].Value.ToString(); string um = this.dataGridView1.Rows[i].Cells[3].Value.ToString(); string quantity = this.dataGridView1.Rows[i].Cells[4].Value.ToString(); string invahj = this.dataGridView1.Rows[i].Cells[6].Value.ToString(); string stkid = this.dataGridView1.Rows[i].Cells[7].Value.ToString(); string stkdesc = this.dataGridView1.Rows[i].Cells[8].Value.ToString(); string state = "4"; string sh = "3"; string remark; remark = this.dataGridView1.Rows[i].Cells[9].Value.ToString(); getcon(); string stkfksql = "select sl from stock_fk where stkid='" + stkid + "' and item='" + item + "' "; SqlDataAdapter sda = new SqlDataAdapter(stkfksql,My_constr); // sda.SelectCommand.Transaction = db.tran; DataTable tmptdt = new DataTable(); sda.Fill(tmptdt); if (tmptdt.Rows.Count<=0) { MessageBox.Show("系统无库存数量"); } else { if (Convert.ToDecimal(quantity) <= Convert.ToDecimal(tmptdt.Rows[0]["sl"].ToString())) { sqlcode = "insert into LSINVA (leiname,lei,invadh,state,sh,hh,item,itemdesc,um,quantity,invahj,stkid,stkdesc,remark,operater) "; sqlcode += " values('" + leiname + "','" + lei + "','" + invadh + "','" + state + "','" + sh + "','" + hh + "','" + item + "','" + itemdesc + "' "; sqlcode += ",'" + um + "','" + quantity + "','" + invahj + "','" + stkid + "','" + stkdesc + "','" + remark + "','" + operater + "')"; db.SQLCOM.CommandText = sqlcode; db.SQLCOM.ExecuteNonQuery(); // MessageBox.Show("库存数量足"); } else { MessageBox.Show("库存数量不足"); //MessageBox.Show(sdr["sl"].ToString()); } } } db.tran.Commit(); //调用事务提交 MessageBox.Show("调整单保存成功", "软件提示"); } catch (Exception ex) { db.tran.Rollback ();//调用事务回滚 MessageBox.Show(ex.ToString(), "软件提示"); MessageBox.Show("调整单保存失败", "软件提示"); }
波仔 2012-12-24
  • 打赏
  • 举报
回复
不会啊,在里面执行多个sqlCmd.ExecuteNonQuery();都没问题的, 你试下 trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
CGabriel 2012-12-24
  • 打赏
  • 举报
回复
重新 new 一个 connection 就是了。。反正 SqlConnection 只是逻辑上的连接而已。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧