C#中事务的使用

View Code
1 private static int nciisMoidyOffFinance(IP_OrderDrivingLicence model_orders, int typeId)
2 {
3 // 查询消费时,保存记录编号
4 int OrderId = 0 ;
5 using (SqlConnection connection = new SqlConnection(SqlHelper.connectionString))
6 {
7 connection.Open();
8 // 使用当前的SQL连接 创建事务并添加共享锁
9 SqlTransaction tran = connection.BeginTransaction(IsolationLevel.ReadCommitted);
10 // 使用当前的SQL连接创建SQL命令并绑定事务
11 SqlCommand command = connection.CreateCommand();
12 command.Transaction = tran;
13 try
14 {
15 // 插入查询记录
16 StringBuilder strSql = new StringBuilder();
17 strSql.Append( " insert into IP_OrderDrivingLicence( " );
18 strSql.Append( " jzofId,jzofunId,jzNo,jzName,jzsubtime,jzstate,jzIsrevalue,jzofamount,jzofRemainamount,jzoperatorId) " );
19 strSql.Append( " values ( " );
20 strSql.Append( " @jzofId,@jzofunId,@jzNo,@jzName,@jzsubtime,@jzstate,@jzIsrevalue,@jzofamount,@jzofRemainamount,@jzoperatorId) " );
21 strSql.Append( " ;select @@IDENTITY " );
22 SqlParameter[] parameters = {
23 new SqlParameter( " @jzofId " , SqlDbType.Int, 4 ),
24 new SqlParameter( " @jzofunId " , SqlDbType.Int, 4 ),
25 new SqlParameter( " @jzNo " , SqlDbType.VarChar, 50 ),
26 new SqlParameter( " @jzName " , SqlDbType.VarChar, 50 ),
27 new SqlParameter( " @jzsubtime " , SqlDbType.DateTime),
28 new SqlParameter( " @jzstate " , SqlDbType.Int, 4 ),
29 new SqlParameter( " @jzIsrevalue " , SqlDbType.Int, 4 ),
30 new SqlParameter( " @jzofamount " , SqlDbType.Decimal, 9 ),
31 new SqlParameter( " @jzofRemainamount " , SqlDbType.Decimal, 9 ),
32 new SqlParameter( " @jzoperatorId " , SqlDbType.Int, 4 )};
33 parameters[ 0 ].Value = model_orders.jzofId;
34 parameters[ 1 ].Value = 23 ;
35 parameters[ 2 ].Value = model_orders.jzNo;
36 parameters[ 3 ].Value = model_orders.jzName;
37 parameters[ 4 ].Value = DateTime.Now;
38 parameters[ 5 ].Value = 0 ;
39 parameters[ 6 ].Value = 0 ;
40 parameters[ 7 ].Value = model_orders.jzofamount;
41 parameters[ 8 ].Value = model_orders.jzofRemainamount;
42 parameters[ 9 ].Value = model_orders.jzoperatorId;
43 // 设置SQL命令的T-SQL语句
44 command.CommandType = CommandType.Text;
45 command.CommandText = strSql.ToString();
46 // 绑定新参数列表
47 command.Parameters.AddRange(parameters);
48 // 执行失败,回滚事务
49 object n = command.ExecuteScalar();
50 if (n == null )
51 {
52 tran.Rollback();
53 connection.Close();
54 // 出现异常中断
55 return 0 ;
56 }
57 else
58 {
59 // 保存查询的记录编号
60 OrderId = Convert.ToInt32(n);
61 }
62 SqlParameter[] parameters1 =
63 {
64 new SqlParameter( " @ofId " ,SqlDbType.Int, 4 ),
65 new SqlParameter( " @ofOrId " ,SqlDbType.VarChar, 50 ),
66 new SqlParameter( " @ofSubjectId " ,SqlDbType.Int, 4 ),
67 new SqlParameter( " @ofType " ,SqlDbType.Int, 4 ),
68 new SqlParameter( " @ofAmount " ,SqlDbType.Decimal, 9 ),
69 new SqlParameter( " @ofOPid " ,SqlDbType.Int, 4 ),
70 new SqlParameter( " @ofText " ,SqlDbType.VarChar, 200 ),
71 new SqlParameter( " @ofNote1 " ,SqlDbType.VarChar, 200 ),
72 new SqlParameter( " @ofNote2 " ,SqlDbType.VarChar, 200 ),
73 new SqlParameter( " @ofNote3 " ,SqlDbType.VarChar, 200 ),
74 new SqlParameter( " @OperKind " ,SqlDbType.Int, 4 ),
75 new SqlParameter( " @Result " ,SqlDbType.Int, 4 )
76 };
77
78 parameters1[ 0 ].Value = model_orders.jzofId;
79 parameters1[ 1 ].Value = " nciisDL " + OrderId;
80 parameters1[ 2 ].Value = 23 ;
81 parameters1[ 3 ].Value = 2 ;
82 parameters1[ 4 ].Value = model_orders.jzofamount;
83 parameters1[ 5 ].Value = 0 ;
84 parameters1[ 6 ].Value = model_orders.jzNote;
85 parameters1[ 7 ].Value = " 驾照查询消费 " ;
86 parameters1[ 8 ].Value = string .Empty;
87 parameters1[ 9 ].Value = string .Empty;
88 parameters[ 10 ].Value = typeId;
89 parameters[ 11 ].Direction = ParameterDirection.Output;
90 // 设置SQL命令的T-SQL语句
91 command.CommandType = CommandType.StoredProcedure;
92 command.CommandText = " exchange_officeFinance " ;
93 // 清空SQL命令中已有的参数列表并重新绑定新的参数列表
94 command.Parameters.Clear();
95 command.Parameters.AddRange(parameters1);
96 command.ExecuteScalar();
97 int result = Convert.ToInt32(parameters[ 11 ].Value);
98 if (result == - 2 )
99 {
100 // 商户账户冻结
101 tran.Rollback();
102 connection.Close();
103 // 出现异常中断
104 return - 2 ;
105 }
106 if (result == - 1 )
107 {
108 // 商户余额不足
109 tran.Rollback();
110 connection.Close();
111 // 出现异常中断
112 return - 1 ;
113 }
114 // 所有操作成功后,提交事务
115 tran.Commit();
116 return OrderId;
117 }
118 catch
119 {
120 tran.Rollback();
121 connection.Close();
122 // 出现异常中断
123 return 0 ;
124 }
125
126 }
127 }

你可能感兴趣的:(C#)