如题 

解决方案 »

  1. using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using Tools;
    using Microsoft.ApplicationBlocks.Data;/// <summary>
    /// Servicer 的摘要说明
    /// </summary>
    public class ServicerBLL
    {
        /// <summary>
        /// 维修人员提出一个维修申请,需要操作3张表
        /// 1、预约表Reservation
        /// 2、流程处理process
        /// 3、日志Log
        /// 注:用事务的模式完成
        /// </summary>
        /// <param name="rInfo"></param>
        /// <param name="lInfo"></param>
        /// <param name="pInfo"></param>
        /// <param name="Message"></param>
        /// <returns></returns>
        public static bool AddRequest(ReservationInfo rInfo, LogInfo lInfo,processInfo PInfo, out string Message)
        {
            string Sql = string.Empty;
            Message = string.Empty;
            using (SqlConnection conn = DALUtil.GetConnection())
            {
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region Insert into Reservation
                        Sql = "Insert into Reservation(ReservationNo,GuestName,Tel,MaintainDate," +
                    "FaultAddress,FaultDesc,RequestUserId,AuditDate,AuditResult," +
                    "IsSendCar)values(@ReservationNo,@GuestName,@Tel,@MaintainDate,@FaultAddress,@FaultDesc,@RequestUserId,@AuditDate,@AuditResult,@IsSendCar)";                    SqlParameter[] arParms = new SqlParameter[10];                    arParms[0] = new SqlParameter("@ReservationNo", SqlDbType.VarChar, 50);
                        arParms[0].Value = rInfo.ReservationNo;
                        arParms[1] = new SqlParameter("@GuestName", SqlDbType.VarChar, 50);
                        arParms[1].Value = rInfo.GuestName;
                        arParms[2] = new SqlParameter("@Tel", SqlDbType.VarChar, 50);
                        arParms[2].Value = rInfo.Tel;
                        arParms[3] = new SqlParameter("@MaintainDate", SqlDbType.DateTime);
                        arParms[3].Value = rInfo.MaintainDate;
                        arParms[4] = new SqlParameter("@FaultAddress", SqlDbType.VarChar, 255);
                        arParms[4].Value = rInfo.FaultAddress;
                        arParms[5] = new SqlParameter("@FaultDesc", SqlDbType.VarChar, 255);
                        arParms[5].Value = rInfo.FaultDesc;
                        arParms[6] = new SqlParameter("@RequestUserId", SqlDbType.Int);
                        arParms[6].Value = rInfo.RequestUserId;
                        arParms[7] = new SqlParameter("@AuditDate", SqlDbType.DateTime);
                        arParms[7].Value = rInfo.AuditDate;
                        arParms[8] = new SqlParameter("@AuditResult", SqlDbType.Char, 10);
                        arParms[8].Value = rInfo.AuditResult;
                        arParms[9] = new SqlParameter("@IsSendCar", SqlDbType.Bit);
                        arParms[9].Value = rInfo.IsSendCar;                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, Sql,arParms);
                        #endregion                    #region Insert into process
                        Sql = "insert into Process (ProcessUserId,SendUserId,SendDate,Sate,ProcessPage)" +
                        " values(@ProcessUserId,@SendUserId,@SendDate,@Sate,@ProcessPage)";
                        SqlParameter[] arparmsProcess = new SqlParameter[5];
                        arparmsProcess[0] = new SqlParameter("@ProcessUserId", SqlDbType.Int);
                        arparmsProcess[0].Value = PInfo.ProcessUserId;
                        arparmsProcess[1] = new SqlParameter("@SendUserId", SqlDbType.Int);
                        arparmsProcess[1].Value = PInfo.SendUserId;
                        arparmsProcess[2] = new SqlParameter("@SendDate", SqlDbType.DateTime, 8);
                        arparmsProcess[2].Value = PInfo.SendDate;
                        arparmsProcess[3] = new SqlParameter("@Sate", SqlDbType.Bit);
                        arparmsProcess[3].Value = PInfo.Sate;
                        arparmsProcess[4] = new SqlParameter("@ProcessPage", SqlDbType.VarChar, 50);
                        arparmsProcess[4].Value = PInfo.ProcessPage;                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, arparmsProcess);
                        #endregion                    #region Insert into Log
                        Sql = "Insert into Log(UserId,OperationDesc,IP,OperateDate)values(@UserId,@OperationDesc,@IP,@OperateDate)";
                        SqlParameter[] arparmsLog = new SqlParameter[4];
                        arparmsLog[0] = new SqlParameter("@UserId", SqlDbType.Int);
                        arparmsLog[0].Value = lInfo.UserId;
                        arparmsLog[1] = new SqlParameter("@OperationDesc", SqlDbType.VarChar, 255);
                        arparmsLog[1].Value = lInfo.OperationDesc;
                        arparmsLog[2] = new SqlParameter("@IP", SqlDbType.VarChar, 25);
                        arparmsLog[2].Value = lInfo.IP;
                        arparmsLog[3] = new SqlParameter("@OperateDate", SqlDbType.DateTime, 8);
                        arparmsLog[3].Value = lInfo.OperateDate;                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, arparmsLog);
                        #endregion                    trans.Commit();
                        Message = "保存成功";
                        return true;
                    }
                    catch(Exception ex)//出现了错误 
                    {
                        //如果发生错误,就回滚,事物内所有的操作会还原。
                        trans.Rollback();
                        Message += "保存失败<br>" + ex.Message;
                        return false;
                    }
                }
            }
        }
      

  2.  
    delare @error1 int
       begin tran
        --此处是你的执行sql
       set [email protected]@error --是否发生错误
       if(@error=0)
      begin 
         --提交事务
         commit tran
      end
     else
      begin
        rollback tran
      end
      

类似问题 »