最近几个月一直在做报价系统(三层架构,原先是6层架构,由于不好维护,工作量大,后改为三层),需要把报价用户的每一次对数据库的操作都要以日志的记录,方便供应商和维护人员查询操作记录,现举一个发布报价的例子(用存储过程书写)
1,存储过程(可以看到此存储过程用到事物)如下:
存储过程
1 USE [xb_quotation_dev]
2 GO
3
4 /****** Object: StoredProcedure [dbo].[usp_publishtodayQuotation] Script Date: 02/04/2012 13:46:03 ******/
5 SET ANSI_NULLS ON
6 GO
7
8 SET QUOTED_IDENTIFIER ON
9 GO
10
11 CREATE PROCEDURE [dbo].[usp_publishtodayQuotation]
12 @PriceDate Datetime,
13 @AreaID int,
14 @TypeId int
15 AS
16 BEGIN
17 SET NOCOUNT ON;
18 DECLARE @ERRORSUM INT =0;
19 Begin Tran
20 ---把MD_Prices表中不是今天的数据删除
21 SET @ERRORSUM=-1;
22 DELETE FROM MD_Prices WHERE CONVERT(VARCHAR(10),PriceDate,120)<>CONVERT(VARCHAR(10),@PriceDate,120)
23 IF(@@ERROR<>0) GOTO ERROR_HANDLE;
24 --把MD_PriceCache表中不是今天的数据删除
25 SET @ERRORSUM=-2;
26 DELETE FROM MD_PriceCache WHERE CONVERT(VARCHAR(10),PriceDate,120)<>CONVERT(VARCHAR(10),@PriceDate,120)
27 IF(@@ERROR<>0) GOTO ERROR_HANDLE;
28 SET @ERRORSUM=-3;--添加到今日报价出差
29 create table #tbArea(AreaID int)
30 Insert into #tbArea SELECT AreaId FROM MD_Area WHERE QuotationType=@AreaID
31 create table #tbGoods(ID int)
32 Insert into #tbGoods SELECT ID FROM MD_Goods WHERE TypeId = @TypeId
33 INSERT INTO #tbArea VALUES(@AreaID)
34 INSERT INTO MD_Prices SELECT * FROM MD_PriceCache WHERE CONVERT(VARCHAR(10),PriceDate,120)=CONVERT(VARCHAR(10),@PriceDate,120) AND AreaID IN (SELECT AreaID FROM #tbArea) AND GoodsId IN( SELECT ID FROM #tbGoods)
35 IF(@@ERROR<>0) GOTO ERROR_HANDLE;
36 SET @ERRORSUM=-4;--更新报价日志数据错误
37 UPDATE MD_PriceGenerationLog SET publishedDate=GETDATE(),publishedUserName=-1 where CONVERT(varchar(10),generateDate,120)=CONVERT(varchar(10),getdate(),120)
38 drop table #tbArea;
39 drop table #tbGoods;
40 commit tran;
41 return 0;
42 ERROR_HANDLE:
43 rollback tran;
44 RETURN @ERRORSUM;
45 END
46
47
48
49
50 GO
2,下面分别在DAL,BLL,UI实现此功能
DAL层:
OperateLog
1 public class OperateLog
2 {
3 public OperateLog()
4 {
5 }
6 public void AddSysLog(Model.SysModelLog model)
7 {
8 string insertSql = "INSERT INTO MD_SysLog([AdminUserId],[AdminUserName],[Content],[CreateTime],[LogType]) VALUES(@AdminUserID,@AdminUserName,@Content,GETDATE(),@LogType)";
9 SqlParameter[] sp = {
10 new SqlParameter("@AdminUserID",model.AdminUserId),
11 new SqlParameter("@AdminUserName",model.AdminUserName),
12 new SqlParameter("@Content",model.Content),
13 new SqlParameter("@LogType",model.LogType)
14 };
15 DBHelperSQL.ExcuteNonQuery(insertSql, sp);
16 }
17 }
OperateQuotation
1 public class OperateQuotation
2 {
3 public OperateQuotation() { }
4 public int PublishQuotation(string PriceDate, int areaID, int typeID)
5 {
6 string storeName = "usp_publishtodayQuotation";
7 SqlParameter[] sp = {
8 new SqlParameter("@PriceDate",PriceDate),
9 new SqlParameter("@AreaID",areaID),
10 new SqlParameter("@TypeId",typeID)
11 };
12 return DBHelperSQL.RunStoreProce(storeName, sp);
13 }
14 }
BLL层
OperateLog
1 public class OperateLog
2 {
3 public readonly DAL.OperateLog dal = new DAL.OperateLog();
4 public OperateLog() { }
5 public void AddSysLog(string content,int userID,short shTypeID,string userName)
6 {
7 Model.SysModelLog model = new Model.SysModelLog();
8 model.AdminUserId = userID;
9 model.AdminUserName = userName;
10 model.LogType = shTypeID;
11 model.Content = content;
12 dal.AddSysLog(model);
13 }
14 }
OperateQuotation
1 public class OperateQuotation
2 {
3 public readonly DAL.OperateQuotation dal = new DAL.OperateQuotation();
4 public readonly BLL.OperateLog dalLog = new BLL.OperateLog();
5 public OperateQuotation() { }
6
7 public bool PublishQutation(string priceDate, int areaID, int typeID, int userID, string userName)
8 {
9 try
10 {
11
12 int rowsAffects = dal.PublishQuotation(priceDate, areaID, typeID);
13 if (rowsAffects == 0) { return true; }
14 else if (rowsAffects == -1) { dalLog.AddSysLog("把MD_Prices表中不是今天的数据删除出错!", userID, -1, userName); return false; }
15 else if (rowsAffects == -2) { dalLog.AddSysLog("把MD_PriceCache表中不是今天的数据删出错!", userID, -2, userName); return false; }
16 else if (rowsAffects == -3) { dalLog.AddSysLog("添加到今日报价出错!", userID, -3, userName); return false; }
17 else if (rowsAffects == -4) { dalLog.AddSysLog("更新报价日志数据错误", userID, -4, userName); return false; }
18 else
19 {
20
21 return false;
22 }
23 }
24 catch (Exception ex)
25 {
26 dalLog.AddSysLog(ex.StackTrace, userID, 1, userName);
27 throw;
28 }
29 }
30 }
UI层
UI
1 if (bll.PublishQutation("2012-02-03", 15, 1, -1, "打酱油"))
2 {
3 Response.Write("OK");
4 }
Model层
SysModelLog
1 public class SysModelLog
2 {
3 private int _id;
4 private int _adminUserId;
5 private string _adminUserName;
6 private string _content;
7 private int _logType;
8 private DateTime _createDateTime;
9 public int Id
10 {
11 get { return _id; }
12 set { _id = value; }
13 }
14 public int AdminUserId
15 {
16 get { return _adminUserId; }
17 set { _adminUserId = value; }
18 }
19 public string AdminUserName
20 {
21 get { return _adminUserName; }
22 set { _adminUserName = value; }
23 }
24 public string Content
25 {
26 get { return _content; }
27 set { _content = value; }
28 }
29 public int LogType
30 {
31 get { return _logType; }
32 set { _logType = value; }
33 }
34 public DateTime CreateDateTime
35 {
36 get { return _createDateTime; }
37 set { _createDateTime = value; }
38 }
39 }
可以看到在发布报价时候 时刻记录了报价发布情况!