代码中使用事务前提:务必保证一个功能(或用例)在同一个打开的数据连接上,放到同一个事务里面操作。 |
首先是在D层添加一个类为了保存当前操作的这一个连接放到一个事务中执行,并事务执行打开同一个连接、事务完成关闭同一个连接的一个共有类 |
[csharp] view plaincopyprint? |
01.<span style= "font-size:18px;" > using System; |
02. using System.Collections.Generic; |
03. using System.Linq; |
04. using System.Text; |
05. using System.Data; |
06. using System.Data.SqlClient; |
07. using Maticsoft.DBUtility; |
08. namespace PersonalFiles.DAL |
09.{ |
10. public class DBTransaction |
11. { |
12. private DbHelperSQL SqlHelper = null ; |
13. |
14. |
15. |
16. public DBTransaction() |
17. { |
18. SqlHelper = new DbHelperSQL(); |
19. } |
20. |
21. /// <summary> |
22. /// 获取数据库连接 |
23. /// </summary> |
24. /// <returns></returns> |
25. public SqlConnection GetConnection() |
26. { |
27. return SqlHelper.GetCon(); |
28. } |
29. |
30. /// <summary> |
31. /// 获取事务 |
32. /// </summary> |
33. /// <returns></returns> |
34. public SqlTransaction GetTransaction(SqlConnection conn) |
35. { |
36. return conn.BeginTransaction(); |
37. } |
38. |
39. /// <summary> |
40. /// 提交事务 |
41. /// </summary> |
42. public void Commit(SqlTransaction sqlTransaction) |
43. { |
44. sqlTransaction.Commit(); |
45. } |
46. |
47. /// <summary> |
48. /// 回滚事务 |
49. /// </summary> |
50. public void Rollback(SqlTransaction sqlTransaction) |
51. { |
52. sqlTransaction.Rollback(); |
53. } |
54. |
55. /// <summary> |
56. /// 关闭连接 |
57. /// </summary> |
58. public void Close(SqlConnection conn) |
59. { |
60. |
61. if (conn.State == ConnectionState.Open) |
62. { |
63. conn.Close(); |
64. } |
65. |
66. } |
67. } |
68.} |
69.</span> |
界面层的后台代码和以前一样直接调去就行了,现在来看主要是在B层中的代码发生了很大的变化,需要向下层传递事务与获取的连接 |
[csharp] view plaincopyprint? |
01.<span style= "font-size:18px;" > /// <summary> |
02. /// 增加一条数据 |
03. /// </summary> |
04. public void Add(PersonalFiles.Model.BasicInformation modelBasic, PersonalFiles.Model.T_HumanAgency model) |
05. { |
06. int flag = 0; |
07. |
08. DBTransaction DbTran = new DBTransaction(); |
09. |
10. |
11. //获得连接 |
12. SqlConnection conn = DbTran.GetConnection(); |
13. |
14. |
15. |
16. //开启事务 |
17. SqlTransaction trans = DbTran.GetTransaction(conn); |
18. try |
19. { |
20. //把获得的同一个连接与事务一共传下去 |
21. //dalBasic.Add(modelBasic,conn,trans); |
22. |
23. //把获得的同一个连接与事务一共传下去 |
24. |
25. dalAgency.Add(model,conn,trans); |
26. |
27. |
28. |
29. |
30. //事务提交 |
31. DbTran.Commit(trans); |
32. //return true; |
33. } |
34. |
35. catch (Exception ex) |
36. { |
37. //回滚事务 |
38. DbTran.Rollback(trans); |
39. } |
40. finally |
41. { |
42. DbTran.Close(conn); |
43. } |
44. }</span> |
注意的是向D层传是我们需要传的是B层获取的同一个连接于开启的是一个事务: |
[csharp] view plaincopyprint? |
01.<span style= "font-size:18px;" > /// <summary> |
02. /// 增加一条数据 |
03. /// </summary> |
04. public void Add(PersonalFiles.Model.T_HumanAgency model,SqlConnection conn,SqlTransaction trans) |
05. { |
06. StringBuilder strSql = new StringBuilder(); |
07. strSql.Append( "insert into T_HumanAgency(" ); |
08. strSql.Append( "myidentity,relation,receivemode,workingtime,intotime,oldworkplace,nowworkplace,inervice,registered,registeredcardid,registeredid,householder,isrecord,fileintotime,fileouttime,filetowhere,relationouttime,Paymentstandard,paymentsmonth,payments,stoptime,state,pri,admin,ID)" ); |
09. strSql.Append( " values (" ); |
10. strSql.Append( "@myidentity,@relation,@receivemode,@workingtime,@intotime,@oldworkplace,@nowworkplace,@inervice,@registered,@registeredcardid,@registeredid,@householder,@isrecord,@fileintotime,@fileouttime,@filetowhere,@relationouttime,@Paymentstandard,@paymentsmonth,@payments,@stoptime,@state,@pri,@admin,@ID)" ); |
11. SqlParameter[] parameters = { |
12. new SqlParameter( "@myidentity" , SqlDbType.VarChar,50), |
13. new SqlParameter( "@relation" , SqlDbType.VarChar,50), |
14. new SqlParameter( "@receivemode" , SqlDbType.VarChar,50), |
15. new SqlParameter( "@workingtime" , SqlDbType.VarChar,50), |
16. new SqlParameter( "@intotime" , SqlDbType.VarChar,50), |
17. new SqlParameter( "@oldworkplace" , SqlDbType.VarChar,50), |
18. new SqlParameter( "@nowworkplace" , SqlDbType.VarChar,50), |
19. new SqlParameter( "@inervice" , SqlDbType.VarChar,50), |
20. new SqlParameter( "@registered" , SqlDbType.VarChar,50), |
21. new SqlParameter( "@registeredcardid" , SqlDbType.VarChar,50), |
22. new SqlParameter( "@registeredid" , SqlDbType.VarChar,50), |
23. new SqlParameter( "@householder" , SqlDbType.VarChar,50), |
24. new SqlParameter( "@isrecord" , SqlDbType.VarChar,50), |
25. new SqlParameter( "@fileintotime" , SqlDbType.VarChar,50), |
26. new SqlParameter( "@fileouttime" , SqlDbType.VarChar,50), |
27. new SqlParameter( "@filetowhere" , SqlDbType.VarChar,50), |
28. new SqlParameter( "@relationouttime" , SqlDbType.VarChar,50), |
29. new SqlParameter( "@Paymentstandard" , SqlDbType.VarChar,50), |
30. new SqlParameter( "@paymentsmonth" , SqlDbType.VarChar,50), |
31. new SqlParameter( "@payments" , SqlDbType.VarChar,50), |
32. new SqlParameter( "@stoptime" , SqlDbType.VarChar,50), |
33. new SqlParameter( "@state" , SqlDbType.VarChar,50), |
34. new SqlParameter( "@admin" , SqlDbType.VarChar,50), |
35. new SqlParameter( "@pri" , SqlDbType.VarChar,50), |
36. new SqlParameter( "@ID" , SqlDbType.VarChar,50)}; |
37. parameters[0].Value = model.myidentity; |
38. parameters[1].Value = model.relation; |
39. parameters[2].Value = model.receivemode; |
40. parameters[3].Value = model.workingtime; |
41. parameters[4].Value = model.intotime; |
42. parameters[5].Value = model.oldworkplace; |
43. parameters[6].Value = model.nowworkplace; |
44. parameters[7].Value = model.inervice; |
45. parameters[8].Value = model.registered; |
46. parameters[9].Value = model.registeredcardid; |
47. parameters[10].Value = model.registeredid; |
48. parameters[11].Value = model.householder; |
49. parameters[12].Value = model.isrecord; |
50. parameters[13].Value = model.fileintotime; |
51. parameters[14].Value = model.fileouttime; |
52. parameters[15].Value = model.filetowhere; |
53. parameters[16].Value = model.relationouttime; |
54. parameters[17].Value = model.Paymentstandard; |
55. parameters[18].Value = model.paymentsmonth; |
56. parameters[19].Value = model.payments; |
57. parameters[20].Value = model.stoptime; |
58. parameters[21].Value = model.state; |
59. parameters[22].Value = model.pri; |
60. parameters[23].Value = model.admin; |
61. parameters[24].Value = model.ID; |
62. |
63. |
64. //DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); |
65. DbHelperSQL.ExecuteSql(strSql.ToString(),conn,trans, parameters); |
66. }</span> |
在代码中添加事务与存储过程中添加事务的异同 |
相同点 |
1:都能够保证数据的一致性。 |
不同点: |
1:代码中添加事务的好处是:增加了代码的可读性、与可维护性,方便后期人员维护系统看代码能够一目了然的看懂代码,而在数据库中添加存储过程的可读性不是很好。 |
2:为什么不建议使用数据库自带的存储过程+事务呢?主要是一个项目过多的依赖数据库,这样对后期的数据库迁移都会带来一定的影响与不便(sql向oracle迁移),好多转换不是很容易兼容性和不是很好(以后再深入学习)。 |
3:合作开发时如果是代码中添加事务遵循了代码上传的原则,这样方便大家的交流。 |
为什么使用事务可以保证同一个连接向数据库多个表写信息的正确性与一致性的原理: |
事务的原子性 |
事务的原子性指的是,事务中包含的程序作为数据库的逻辑工作单位,它所做的对数据改操作要全部执行,要么全部不执行。这种特性称为原子性。 事务的原子性要求,如果把一个事务看作是一个程序,它要么完整的被执行,要么完全执行。就是说事务的操纵序列或者完全应用到数据库或者完全不影响数据库。这种特性称为原则性 假如用户在一个事务内完成了对数据库的更新,这时所有的更新对外部世界必须是可见的,或者完全没有更新。前者称事务已提交,后者称事务撤销。DBMS必须确保由成功提交的事物完成的所有操作在数据库内有完全的反映,而失败的事务对数据库完全没有影响 |
事务的隔离性 |
事务开始执行了但是没有提交事务,数据并没有真正的写到数据库里面,当我去断点测试的时候当第一个程序向数据库发出写完时,我去查找数据库不能打开数据库,不能查找,提示连接超时,由于事务的隔离性,数据并没有真正的写到数据库里面,等事务提交才可以查到数据库,可见同一个连接下执行的程序在同一个事务执行的开始于结束后才真正写到数据库里面,如果过程当中保存事务回滚,数据不会写到数据库里面。保证数据的一致性与正确性。 |
数据的准确性与一致性是我们要时刻考虑的,一个好的系统必须有较好的准确性才能保证用户的使用。 |
by: 发表于:2018-01-12 14:35:18 顶(0) | 踩(0) 回复
??
回复评论