事物处理方法

2019-04-15 16:49发布

string qlcadd = "select A_AddMans,A_DriverImage,A_AddDates from QLCAdds where A_CardId='" + Gid + "'";
            DataTable dt = DBHelper.GetDataSet(qlcadd);
            byte[] pic = (byte[])dt.Rows[0]["A_DriverImage"];
            string stradd = "INSERT INTO 资格证审核完成明细表 (申请编号, 驾驶员姓名, 身份证号, 证件归属区域, 证件住址, 现住址, 驾驶证号, 驾驶证办理日期, 驾驶员照片, 性别, 申请经办人, 申请日期, 备注,驾驶员手机, 审核完成时间) values ('" + Gid + "','" + Label3.Text + "','" + Label4.Text + "','" + Label5.Text + "','" + Label6.Text + "','" + Label7.Text + "','" + Label8.Text + "','" + Label9.Text + "',@pic,'" + Label14.Text + "','" + dt.Rows[0]["A_AddMans"] + "','" + dt.Rows[0]["A_AddDates"] + "','" + TextBox1.Text + "','" + Label13.Text + "','" + DateTime.Now + "')";
            SqlParameter[] parameters = {
      new SqlParameter("@pic",SqlDbType.Binary)};
            parameters[0].Value = pic;
           
            string str = "update 出租管理处意见明细表 set 落实状态='" + DropDownList2.SelectedValue + "',经办日期='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "' where 意见项目='" + Label11.Text + "' and 审核资格证编号='" + Gid + "'";
            string strdelete = "delete from QLCAdds where A_CardId='" + Gid + "'";
            string[] arrsql = new string[3];
            arrsql[0] = stradd;
            arrsql[1] = str;
            arrsql[2] = strdelete;
            DBHelper.GetTransaction(arrsql,parameters);      ///
        /// 事务处理
        ///

        /// SQL语句数组
        /// 执行成功返回1失败返回0
        public static int GetTransaction(string[] sql)
        {
            string SQLString = "";
            SqlCommand tran_cmd = new SqlCommand(SQLString, connection);             try
            {
                tran_cmd.Transaction = Connection.BeginTransaction();
                for (int i = 0; i < sql.Length; i++)
                {
                    tran_cmd.CommandText = sql[i];
                    tran_cmd.CommandType = CommandType.Text;
                    tran_cmd.ExecuteNonQuery();                 }
                //提交事务
                tran_cmd.Transaction.Commit();
                return 1;             }
            catch
            {
                //回滚事务
                tran_cmd.Transaction.Rollback();
                throw;
            }         }         ///
        /// 事务处理
        ///

        /// SQL语句数组
        /// 执行成功返回1失败返回0
        public static int GetTransaction(string[] sql, params SqlParameter[] cmdParms)
        {
            string SQLString = "";
            SqlCommand tran_cmd = new SqlCommand(SQLString, connection);             try
            {
                tran_cmd.Transaction = Connection.BeginTransaction();
                tran_cmd.CommandText = sql[0];
                tran_cmd.CommandType = CommandType.Text;
                tran_cmd.Parameters.AddRange(cmdParms);
                tran_cmd.ExecuteNonQuery();
                tran_cmd.Parameters.Clear();
                for (int i = 1; i < sql.Length; i++)
                {
                    tran_cmd.CommandText = sql[i];
                    tran_cmd.CommandType = CommandType.Text;
                    tran_cmd.ExecuteNonQuery();                 }
                //提交事务
                tran_cmd.Transaction.Commit();
                return 1;             }
            catch
            {
                //回滚事务
                tran_cmd.Transaction.Rollback();
                throw;
            }         }    string stradd = "insert into dbo.资格证审核完成明细表( 申请编号, 驾驶员姓名, 身份证号, 证件归属区域, 证件住址, 现住址, 驾驶证号, 驾驶证办理日期, 驾驶员照片, 性别, 申请经办人, 申请日期, 驾驶员手机, 备注, 审核完成时间)(SELECT  A_CardId, A_DriverName, A_IdCard, A_IdCardAbout, A_IdCardAddress, A_DriverAddress, A_DriverCarId, A_DriverCarIdDate, A_DriverImage, A_DriverSex, A_AddMans, A_AddDates, A_DriverPhone,'"+TextBox1.Text+"','"+DateTime.Now.ToString()+"' FROM    dbo.QLCAdds where A_CardId='" + Gid + "')";