• 事务控制案例(一)


    表示层

     try
                {
                    if (string.IsNullOrEmpty(txtFPNum.Text))
                    {
                        MessageBox.Show("请输入发票号!", "有情提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtFPNum.Focus();
                    }
                    else
                    {
                        if (HisCommonTools.CommonTools.ToolHelper.QuestionMsg("是否入库存盘?"))
                        {
                            //----------------------添加进销存------------------
                            List<Model_Us_ypjxc> modellist = new List<Model_Us_ypjxc>();
                            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                            { 
                                //存DGV外数据
                                model_Us_ypjxc.Djh = txtBillCode.Text.Trim();
                                model_Us_ypjxc.Czlb = comboType.SelectedValue.ToString();
                                model_Us_ypjxc.Basecode = comboDep.SelectedValue.ToString();//主部门
                                model_Us_ypjxc.Offcode = comUnits.SelectedValue.ToString();
                                model_Us_ypjxc.Fph = txtFPNum.Text;
                                model_Us_ypjxc.Czrq = dateTimeInStore.Value;
                                model_Us_ypjxc.Bz = txtSummary.Text;
                                model_Us_ypjxc.Inout = 1; //
                                model_Us_ypjxc.Czy = Lname;
                                //存DGV数据
                                model_Us_ypjxc.medicode = dataGridView1[0, i].Value.ToString();
                                model_Us_ypjxc.Sccj = dataGridView1[2, i].Value.ToString();
                                model_Us_ypjxc.Jj = Convert.ToDecimal(dataGridView1[3, i].Value.ToString());
                                model_Us_ypjxc.Pfj = Convert.ToDecimal(dataGridView1[4, i].Value.ToString());
                                model_Us_ypjxc.Lsj = Convert.ToDecimal(dataGridView1[5, i].Value.ToString());
                                model_Us_ypjxc.Crsl = Convert.ToDecimal(dataGridView1[6, i].Value.ToString());
                                model_Us_ypjxc.Crje = Convert.ToDecimal(dataGridView1[7, i].Value.ToString());
                                model_Us_ypjxc.Bzs = Convert.ToInt32(dataGridView1[8, i].Value.ToString());
                                model_Us_ypjxc.Pch = dataGridView1[9, i].Value.ToString();
                                model_Us_ypjxc.OverData = dataGridView1[10, i].Value.ToString();
                                modellist.Add(model_Us_ypjxc);  
                            }
                            //事务添加批量数据
                            int sucess = bll_Us_ypjxc.Add(modellist);
                            if (sucess < 1)
                            {
                                MessageBox.Show("入库存盘失败!", "有情提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                            else
                            {
                                MessageBox.Show("入库存盘成功!", "有情提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            dataGridView1.Rows.Clear();
                        }
                      }
                      txtBillCode.Text = this.GetDJH();//生成单据号
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, CommonMessage.InputDateErr()); ;
                }

    逻辑层

       public int  Add(List<Model_Us_ypjxc> modellist)
      {
                return  dal.Add(modellist);
      }

    数据层

      public int Add(List<Model_Us_ypjxc> modellist)
      {
                Hashtable SQLStringList = new Hashtable();
                for (int i = 0; i < modellist.Count; i++)
                {
                             SqlParameter[] parameters = {
                                new SqlParameter(string.Format("@Basecode{0}",i),SqlDbType.VarChar,20), 
                               new SqlParameter(string.Format("@Offcode{0}",i),SqlDbType.VarChar,20), 
                               new SqlParameter(string.Format("@Djh{0}",i),SqlDbType.VarChar,30), 
                               new SqlParameter(string.Format("@Czlb{0}",i),SqlDbType.VarChar,6), 
                              new SqlParameter(string.Format("@Inout{0}",i),SqlDbType.Int), 
                               new SqlParameter(string.Format("@Czrq{0}",i),SqlDbType.DateTime), 
                                new SqlParameter(string.Format("@Czy{0}",i),SqlDbType.VarChar,6), 
                               new SqlParameter(string.Format("@Hdr{0}",i),SqlDbType.VarChar,6), 
                               new SqlParameter(string.Format("@Hdrq{0}",i),SqlDbType.DateTime), 
                               new SqlParameter(string.Format("@Stage{0}",i),SqlDbType.VarChar,50), 
                               new SqlParameter(string.Format("@medicode{0}",i),SqlDbType.VarChar,20), 
                              new SqlParameter(string.Format("@Sccj{0}",i),SqlDbType.VarChar,200), 
                               new SqlParameter(string.Format("@Jj{0}",i),SqlDbType.Decimal,9), 
                             new SqlParameter(string.Format("@Pfj{0}",i),SqlDbType.Decimal,9), 
                              new SqlParameter(string.Format("@Lsj{0}",i),SqlDbType.Decimal,9), 
                             new SqlParameter(string.Format("@Crsl{0}",i),SqlDbType.Decimal,9), 
                             new SqlParameter(string.Format("@Crje{0}",i),SqlDbType.Decimal,9), 
                              new SqlParameter(string.Format("@Pch{0}",i),SqlDbType.VarChar,20), 
                             new SqlParameter(string.Format("@Bzs{0}",i),SqlDbType.VarChar,20), 
                           new SqlParameter(string.Format("@Fph{0}",i),SqlDbType.VarChar,20), 
                           new SqlParameter(string.Format("@OverData{0}",i),SqlDbType.VarChar,20),
                         new SqlParameter(string.Format("@Bz{0}",i),SqlDbType.VarChar,200)};

                    parameters[0].Value = modellist[i].Basecode;
                    parameters[1].Value = modellist[i].Offcode;
                    parameters[2].Value = modellist[i].Djh;
                    parameters[3].Value = modellist[i].Czlb;
                    parameters[4].Value = modellist[i].Inout;
                    parameters[5].Value = modellist[i].Czrq;
                    parameters[6].Value = modellist[i].Czy;
                    parameters[7].Value = modellist[i].Hdr;
                    parameters[8].Value = modellist[i].Hdrq;
                    parameters[9].Value = modellist[i].Stage;
                    parameters[10].Value = modellist[i].medicode;
                    parameters[11].Value = modellist[i].Sccj;
                    parameters[12].Value = modellist[i].Jj;
                    parameters[13].Value = modellist[i].Pfj;
                    parameters[14].Value = modellist[i].Lsj;
                    parameters[15].Value = modellist[i].Crsl;
                    parameters[16].Value = modellist[i].Crje;
                    parameters[17].Value = modellist[i].Pch;
                    parameters[18].Value = modellist[i].Bzs;
                    parameters[19].Value = modellist[i].Fph;
                    parameters[20].Value = modellist[i].OverData;
                    parameters[21].Value = modellist[i].Bz;
                    SQLStringList.Add(string.Format("insert into Us_ypjxc (Basecode,Offcode,Djh,Czlb,Inout,Czrq,Czy,Hdr,Hdrq,Stage,medicode,Sccj,Jj,Pfj,Lsj,Crsl,Crje,Pch,Bzs,Fph,OverData,Bz) values (@Basecode{0},@Offcode{0},@Djh{0},@Czlb{0},@Inout{0},@Czrq{0},@Czy{0},@Hdr{0},@Hdrq{0},@Stage{0},@medicode{0},@Sccj{0},@Jj{0},@Pfj{0},@Lsj{0},@Crsl{0},@Crje{0},@Pch{0},@Bzs{0},@Fph{0},@OverData{0},@Bz{0})", i), parameters);
                }
                     return    DbHelperSQL.ExecuteSqlTran(SQLStringList); 
      }

    方法

    /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
            public static int ExecuteSqlTran(Hashtable SQLStringList)
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        SqlCommand cmd = new SqlCommand();
                        try
                        {
                            //循环
                            foreach (DictionaryEntry myDE in SQLStringList)
                            {
                                string cmdText = myDE.Key.ToString();
                                SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
                                PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                                int val = cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            trans.Commit();
                            return 1;
                        }
                        catch
                        {
                            trans.Rollback();
                            return 0;
                            throw;
                        }
                    }
                }
            }

  • 相关阅读:
    getWidth() 和 getMeasuredWidth()的区别
    解决 win7 注册com组件失败问题
    Documentation/PCI/pci-iov-howto.txt
    Android Migrate Android Code
    struts2 18拦截器详解(九)
    使用Maven模板创建项目
    转换基于Maven的Web应用程序支持Eclipse IDE
    转换基于Maven的Java项目支持Eclipse IDE
    Eclipse构建Maven项目
    NetBeans IDE集成Maven
  • 原文地址:https://www.cnblogs.com/leischen/p/2306023.html
Copyright © 2020-2023  润新知