asp.net 3.5 csharp 实现事务代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Data;
8 using System.Data.SqlClient;
9 using System.Globalization;
10 using System.IO;
11 using System.Text;
12 using System.Security.Cryptography;
13 using System.Web.UI.HtmlControls;
14 using System.Transactions;//必需添加引用
15
16
17
18 public partial class transaction : System.Web.UI.Page
19 {
20 private SqlConnection oConn = null;
21
22 private string strMessage;
23
24 private SqlTransaction transactions = null;
25
26 protected void Page_Load(object sender, EventArgs e)
27 {
28 Response.Charset = "utf-8";
29 Response.ContentEncoding = Encoding.UTF8;
30 Session.CodePage = 65001;
31 Page.Header.Title = "缔友计算机信息技术有限公司,涂聚文 너를 환 영한다";
32
33 HtmlMeta meta = new HtmlMeta();
34 meta.Name = "keywords";
35 meta.Content = "捷为工作室,締友計算機信息技術有限公司,塗聚文,开发国际化多种语言,兼容各浏览器的网站,个性化应用软件 너를 환 영한다"; //SiteMap.CurrentNode["keywords"];
36 Page.Header.Controls.AddAt(1, meta);
37
38 HtmlMeta meta1 = new HtmlMeta();
39 meta1.Name = "description";
40 meta1.Content = "China,Guangdong Province,Shenzhen 너를 환 영한다";
41 Page.Header.Controls.AddAt(2, meta1);
42
43 HtmlMeta author = new HtmlMeta();
44 author.Name = "Author";
45 author.Content = "涂聚文 geovindu@163.com";
46 Page.Header.Controls.AddAt(3, author);
47
48 HtmlLink cssLink = new HtmlLink();
49 cssLink.Href = "~/styles.css";
50 cssLink.Attributes.Add("rel", "stylesheet");
51 cssLink.Attributes.Add("type", "text/css");
52 Header.Controls.Add(cssLink);
53
54 HtmlLink ico = new HtmlLink();
55 ico.Href = "favicon.ico";
56 ico.Attributes.Add("rel", "icon");
57 ico.Attributes.Add("type", "image/x-icon");
58 Header.Controls.Add(ico);
59 }
60 /// <summary>
61 /// 操作事务
62 /// </summary>
63 /// <param name="sender"></param>
64 /// <param name="e"></param>
65 protected void Button1_Click(object sender, EventArgs e)
66 {
67 /*
68 --sql脚本事务
69 create procedure proc_EdmEmail_insert
70 as
71 begin
72 begin transaction
73 insert into EdmEamil(e_mail,vipno) values('geovindu@jw28.com','edm00001')
74 insert into EdmEamil(e_mail,vipno) values('463588883@qq.com','edm00002')
75
76 if(@@error<>0)
77 begin
78 --出错后回滚事务
79 rollback transaction
80 return 0;
81 end;
82 --提交事务
83 commit transaction
84 return 1;
85 end;
86 GO
87
88 EXEC proc_EdmEmail_insert
89
90 */
91 try
92 {
93 oConn = new SqlConnection();
94 oConn.ConnectionString = "server=.; database=Vip; uid=sa; pwd=0214;";
95 SqlCommand oCmd = oConn.CreateCommand();
96 oConn.Open();
97 transactions = oConn.BeginTransaction();//啟動一個事務
98 oCmd.Transaction = transactions;
99
100 oCmd.CommandText = "insert into EdmEamil(e_mail,vipno) values( '" + this.TextBox1.Text.Trim() + "','" + this.TextBox2.Text.Trim() + "')";
101 oCmd.ExecuteNonQuery();
102
103
104
105 //如果整個事務操作執行正確,則提效事務
106 transactions.Commit();
107 strMessage = "成功完成";
108 }
109 catch (Exception ex)
110 {
111
112 strMessage = ex.Message.ToString();
113 try
114 {
115 //如果插入數據操作失敗,則事務向前回滾
116 transactions.Rollback();
117 strMessage = strMessage + "事務向前回滾";
118 oConn.Close();
119
120 }
121 catch (Exception exs)
122 {
123 strMessage =strMessage+ exs.Message.ToString();
124 }
125
126 }
127 Response.Write("" + strMessage + "");
128 }
129 /// <summary>
130 ///
131 /// </summary>
132 /// <param name="sender"></param>
133 /// <param name="e"></param>
134 protected void Button2_Click(object sender, EventArgs e)
135 {
136 try
137 {
138 using (TransactionScope scope = new TransactionScope())
139 {
140 oConn = new SqlConnection();
141 oConn.ConnectionString = "server=.; database=Vip; uid=sa; pwd=0214;";
142 SqlCommand oCmd = oConn.CreateCommand();
143 oConn.Open();
144
145 oCmd.CommandText = "insert into EdmEamil(e_mail,vipno) values( '" + this.TextBox1.Text.Trim() + "','" + this.TextBox2.Text.Trim() + "')";
146 oCmd.ExecuteNonQuery();
147
148
149
150 scope.Complete();
151 strMessage = "";
152
153 }
154 }
155 catch(TransactionException ett)
156 {
157 strMessage = "事務出錯";
158 strMessage =strMessage+ ett.Message.ToString();
159 }
160 catch (Exception ex)
161 { strMessage = strMessage + ex.Message.ToString(); }
162 }
163 }
164
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Data;
8 using System.Data.SqlClient;
9 using System.Globalization;
10 using System.IO;
11 using System.Text;
12 using System.Security.Cryptography;
13 using System.Web.UI.HtmlControls;
14 using System.Transactions;//必需添加引用
15
16
17
18 public partial class transaction : System.Web.UI.Page
19 {
20 private SqlConnection oConn = null;
21
22 private string strMessage;
23
24 private SqlTransaction transactions = null;
25
26 protected void Page_Load(object sender, EventArgs e)
27 {
28 Response.Charset = "utf-8";
29 Response.ContentEncoding = Encoding.UTF8;
30 Session.CodePage = 65001;
31 Page.Header.Title = "缔友计算机信息技术有限公司,涂聚文 너를 환 영한다";
32
33 HtmlMeta meta = new HtmlMeta();
34 meta.Name = "keywords";
35 meta.Content = "捷为工作室,締友計算機信息技術有限公司,塗聚文,开发国际化多种语言,兼容各浏览器的网站,个性化应用软件 너를 환 영한다"; //SiteMap.CurrentNode["keywords"];
36 Page.Header.Controls.AddAt(1, meta);
37
38 HtmlMeta meta1 = new HtmlMeta();
39 meta1.Name = "description";
40 meta1.Content = "China,Guangdong Province,Shenzhen 너를 환 영한다";
41 Page.Header.Controls.AddAt(2, meta1);
42
43 HtmlMeta author = new HtmlMeta();
44 author.Name = "Author";
45 author.Content = "涂聚文 geovindu@163.com";
46 Page.Header.Controls.AddAt(3, author);
47
48 HtmlLink cssLink = new HtmlLink();
49 cssLink.Href = "~/styles.css";
50 cssLink.Attributes.Add("rel", "stylesheet");
51 cssLink.Attributes.Add("type", "text/css");
52 Header.Controls.Add(cssLink);
53
54 HtmlLink ico = new HtmlLink();
55 ico.Href = "favicon.ico";
56 ico.Attributes.Add("rel", "icon");
57 ico.Attributes.Add("type", "image/x-icon");
58 Header.Controls.Add(ico);
59 }
60 /// <summary>
61 /// 操作事务
62 /// </summary>
63 /// <param name="sender"></param>
64 /// <param name="e"></param>
65 protected void Button1_Click(object sender, EventArgs e)
66 {
67 /*
68 --sql脚本事务
69 create procedure proc_EdmEmail_insert
70 as
71 begin
72 begin transaction
73 insert into EdmEamil(e_mail,vipno) values('geovindu@jw28.com','edm00001')
74 insert into EdmEamil(e_mail,vipno) values('463588883@qq.com','edm00002')
75
76 if(@@error<>0)
77 begin
78 --出错后回滚事务
79 rollback transaction
80 return 0;
81 end;
82 --提交事务
83 commit transaction
84 return 1;
85 end;
86 GO
87
88 EXEC proc_EdmEmail_insert
89
90 */
91 try
92 {
93 oConn = new SqlConnection();
94 oConn.ConnectionString = "server=.; database=Vip; uid=sa; pwd=0214;";
95 SqlCommand oCmd = oConn.CreateCommand();
96 oConn.Open();
97 transactions = oConn.BeginTransaction();//啟動一個事務
98 oCmd.Transaction = transactions;
99
100 oCmd.CommandText = "insert into EdmEamil(e_mail,vipno) values( '" + this.TextBox1.Text.Trim() + "','" + this.TextBox2.Text.Trim() + "')";
101 oCmd.ExecuteNonQuery();
102
103
104
105 //如果整個事務操作執行正確,則提效事務
106 transactions.Commit();
107 strMessage = "成功完成";
108 }
109 catch (Exception ex)
110 {
111
112 strMessage = ex.Message.ToString();
113 try
114 {
115 //如果插入數據操作失敗,則事務向前回滾
116 transactions.Rollback();
117 strMessage = strMessage + "事務向前回滾";
118 oConn.Close();
119
120 }
121 catch (Exception exs)
122 {
123 strMessage =strMessage+ exs.Message.ToString();
124 }
125
126 }
127 Response.Write("" + strMessage + "");
128 }
129 /// <summary>
130 ///
131 /// </summary>
132 /// <param name="sender"></param>
133 /// <param name="e"></param>
134 protected void Button2_Click(object sender, EventArgs e)
135 {
136 try
137 {
138 using (TransactionScope scope = new TransactionScope())
139 {
140 oConn = new SqlConnection();
141 oConn.ConnectionString = "server=.; database=Vip; uid=sa; pwd=0214;";
142 SqlCommand oCmd = oConn.CreateCommand();
143 oConn.Open();
144
145 oCmd.CommandText = "insert into EdmEamil(e_mail,vipno) values( '" + this.TextBox1.Text.Trim() + "','" + this.TextBox2.Text.Trim() + "')";
146 oCmd.ExecuteNonQuery();
147
148
149
150 scope.Complete();
151 strMessage = "";
152
153 }
154 }
155 catch(TransactionException ett)
156 {
157 strMessage = "事務出錯";
158 strMessage =strMessage+ ett.Message.ToString();
159 }
160 catch (Exception ex)
161 { strMessage = strMessage + ex.Message.ToString(); }
162 }
163 }
164