Exemplo n.º 1
0
 /// <summary>
 /// 执行删除数据并处理相关数据一致性
 /// </summary>
 /// <param name="e">传入的带有数据的事件参数</param>
 /// <returns></returns>
 private bool DeleteData(ListViewDeleteEventArgs e)
 {
     //实例化数据适配器
     using (var daContent = new t_product_notice_contentTableAdapter())
     //取得数据库连接
     using (var conn = daContent.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置事务
             daContent.Transaction = tran;
             try
             {
                 //当前单号
                 var billNum = Convert.ToString(e.Keys[0]);
                 //当前行号
                 byte rowId = Convert.ToByte(e.Keys[1]);
                 //根据单号和行号删除记录
                 if (daContent.Delete(billNum, rowId) <= 0)
                 {
                     throw new Exception("根据单号删除内容记录失败!");
                 }
                 //检测当前记录内容行数
                 int? iCount = (int?)daContent.CountByBillNum(billNum);
                 if (iCount.HasValue && iCount.Value <= 0)
                 {
                     //表头适配器
                     using (var daHead = new t_product_notice_headTableAdapter())
                     {
                         //设置事务
                         daHead.Transaction = daContent.Transaction = tran;
                         //删除表头
                         if (daHead.Delete(billNum) <= 0)
                         {
                             throw new Exception("根据单号删除表头记录失败!");
                         }
                     }
                 }
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw new Exception("删除入仓单记录出现错误:\n" + ex.Message);
             }
         }
     }
 }