c# mysql代码中写事务_代码中添加事务控制 VS(数据库存储过程+事务) 保证数据的完整性与一致性...
[c#]代碼庫代碼中使用事務(wù)前提:務(wù)必保證一個功能(或用例)在同一個打開的數(shù)據(jù)連接上,放到同一個事務(wù)里面操作。
首先是在D層添加一個類為了保存當(dāng)前操作的這一個連接放到一個事務(wù)中執(zhí)行,并事務(wù)執(zhí)行打開同一個連接、事務(wù)完成關(guān)閉同一個連接的一個共有類
[csharp] view plaincopyprint?
01.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. ///
22. /// 獲取數(shù)據(jù)庫連接
23. ///
24. ///
25. public SqlConnection GetConnection()
26. {
27. return SqlHelper.GetCon();
28. }
29.
30. ///
31. /// 獲取事務(wù)
32. ///
33. ///
34. public SqlTransaction GetTransaction(SqlConnection conn)
35. {
36. return conn.BeginTransaction();
37. }
38.
39. ///
40. /// 提交事務(wù)
41. ///
42. public void Commit(SqlTransaction sqlTransaction)
43. {
44. sqlTransaction.Commit();
45. }
46.
47. ///
48. /// 回滾事務(wù)
49. ///
50. public void Rollback(SqlTransaction sqlTransaction)
51. {
52. sqlTransaction.Rollback();
53. }
54.
55. ///
56. /// 關(guān)閉連接
57. ///
58. public void Close(SqlConnection conn)
59. {
60.
61. if (conn.State == ConnectionState.Open)
62. {
63. conn.Close();
64. }
65.
66. }
67. }
68.}
69.
界面層的后臺代碼和以前一樣直接調(diào)去就行了,現(xiàn)在來看主要是在B層中的代碼發(fā)生了很大的變化,需要向下層傳遞事務(wù)與獲取的連接
[csharp] view plaincopyprint?
01.///
02. /// 增加一條數(shù)據(jù)
03. ///
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. //開啟事務(wù)
17. SqlTransaction trans = DbTran.GetTransaction(conn);
18. try
19. {
20. //把獲得的同一個連接與事務(wù)一共傳下去
21. //dalBasic.Add(modelBasic,conn,trans);
22.
23. //把獲得的同一個連接與事務(wù)一共傳下去
24.
25. dalAgency.Add(model,conn,trans);
26.
27.
28.
29.
30. //事務(wù)提交
31. DbTran.Commit(trans);
32. //return true;
33. }
34.
35. catch (Exception ex)
36. {
37. //回滾事務(wù)
38. DbTran.Rollback(trans);
39. }
40. finally
41. {
42. DbTran.Close(conn);
43. }
44. }
注意的是向D層傳是我們需要傳的是B層獲取的同一個連接于開啟的是一個事務(wù):
[csharp] view plaincopyprint?
01.///
02. /// 增加一條數(shù)據(jù)
03. ///
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. }
在代碼中添加事務(wù)與存儲過程中添加事務(wù)的異同
相同點(diǎn)
1:都能夠保證數(shù)據(jù)的一致性。
不同點(diǎn):
1:代碼中添加事務(wù)的好處是:增加了代碼的可讀性、與可維護(hù)性,方便后期人員維護(hù)系統(tǒng)看代碼能夠一目了然的看懂代碼,而在數(shù)據(jù)庫中添加存儲過程的可讀性不是很好。
2:為什么不建議使用數(shù)據(jù)庫自帶的存儲過程+事務(wù)呢?主要是一個項(xiàng)目過多的依賴數(shù)據(jù)庫,這樣對后期的數(shù)據(jù)庫遷移都會帶來一定的影響與不便(sql向oracle遷移),好多轉(zhuǎn)換不是很容易兼容性和不是很好(以后再深入學(xué)習(xí))。
3:合作開發(fā)時如果是代碼中添加事務(wù)遵循了代碼上傳的原則,這樣方便大家的交流。
為什么使用事務(wù)可以保證同一個連接向數(shù)據(jù)庫多個表寫信息的正確性與一致性的原理:
事務(wù)的原子性
事務(wù)的原子性指的是,事務(wù)中包含的程序作為數(shù)據(jù)庫的邏輯工作單位,它所做的對數(shù)據(jù)改操作要全部執(zhí)行,要么全部不執(zhí)行。這種特性稱為原子性。 事務(wù)的原子性要求,如果把一個事務(wù)看作是一個程序,它要么完整的被執(zhí)行,要么完全執(zhí)行。就是說事務(wù)的操縱序列或者完全應(yīng)用到數(shù)據(jù)庫或者完全不影響數(shù)據(jù)庫。這種特性稱為原則性 假如用戶在一個事務(wù)內(nèi)完成了對數(shù)據(jù)庫的更新,這時所有的更新對外部世界必須是可見的,或者完全沒有更新。前者稱事務(wù)已提交,后者稱事務(wù)撤銷。DBMS必須確保由成功提交的事物完成的所有操作在數(shù)據(jù)庫內(nèi)有完全的反映,而失敗的事務(wù)對數(shù)據(jù)庫完全沒有影響
事務(wù)的隔離性
事務(wù)開始執(zhí)行了但是沒有提交事務(wù),數(shù)據(jù)并沒有真正的寫到數(shù)據(jù)庫里面,當(dāng)我去斷點(diǎn)測試的時候當(dāng)?shù)谝粋€程序向數(shù)據(jù)庫發(fā)出寫完時,我去查找數(shù)據(jù)庫不能打開數(shù)據(jù)庫,不能查找,提示連接超時,由于事務(wù)的隔離性,數(shù)據(jù)并沒有真正的寫到數(shù)據(jù)庫里面,等事務(wù)提交才可以查到數(shù)據(jù)庫,可見同一個連接下執(zhí)行的程序在同一個事務(wù)執(zhí)行的開始于結(jié)束后才真正寫到數(shù)據(jù)庫里面,如果過程當(dāng)中保存事務(wù)回滾,數(shù)據(jù)不會寫到數(shù)據(jù)庫里面。保證數(shù)據(jù)的一致性與正確性。
數(shù)據(jù)的準(zhǔn)確性與一致性是我們要時刻考慮的,一個好的系統(tǒng)必須有較好的準(zhǔn)確性才能保證用戶的使用。
總結(jié)
以上是生活随笔為你收集整理的c# mysql代码中写事务_代码中添加事务控制 VS(数据库存储过程+事务) 保证数据的完整性与一致性...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: feign 整合sentinel_Spr
- 下一篇: mysql key buffer_mys