比如 执行一条插入的语句,如果插入成功!在执行一条 查询语句!
分开执行!
怎么才能来程序中执行的时候,如果插入执行成功! 而查询没有成功! 则插入也就不能插入进去?
必须全部执行成功!才能插入和查询出来!
------解决方案--------------------------------------------------------
用RollBack可以回滚吧
------解决方案--------------------------------------------------------
启用事务。如果中间有某条记录插入失败则Rollback,之前的全部插入操作都撤销。只有全部插入成功时才Commit到数据库。
------解决方案--------------------------------------------------------
如果是同一条数据,我很费解怎么会插入成功而查询不到了,如果真查不到回滚哪条数据呢?
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
sql中可以拼接多条添加语句到一起吧 用事务执行 。。。
------解决方案--------------------------------------------------------
- C# code
private void toolStripButton3_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count <= 0) { MessageBox.Show("你没有选择删除项!", "删除提示"); return; } DialogResult result = MessageBox.Show("是否删除选定项\r\n删除不可恢复!", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (result == DialogResult.OK) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) { sb.Append(dataGridView1.SelectedRows[i].Cells["id"].Value); sb.Append(","); } //MessageBox.Show( sb.ToString().Substring(0, sb.Length - 1)); Material delm = new Material(); delm.DeleteList(sb.ToString().Substring(0, sb.Length - 1)); bindlist(); } } #region 修改时查询物料是否存在2 /// <summary> /// 修改时查询物料是否存在2 /// </summary> /// <param name="Material"></param> /// <returns></returns> public bool CEMID(Material CMID) { string sql = string.Format("select MID from [T_material] where MID ='{0}' and id <>{1}", CMID.MID, CMID.id); if (DataBase.GetRowCount(sql) > 0) return true; else return false; } #endregion
------解决方案--------------------------------------------------------
using(TransactionScope ts=new TransactionScope())
{
yourMethod1();
yourMethod2();
ts.Complete();
}
void yourMethod1()
{
...Db operations
if(xxx)
{
throw new Exception();
}
}
void yourMethod2()
{
...transaction supported wcf service
if(xxx)
{
throw new Exception();
}
}
------解决方案--------------------------------------------------------
百度 事务!!!!
------解决方案--------------------------------------------------------
业务逻辑有问题吧,插入成功肯定能查得到呀,插入不成功肯定查不到,只看插入成功与否就好了吧.
------解决方案--------------------------------------------------------
把事务加到最外面,就是执行方法的外面
都成功了再提交