加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

C#实现的事务

发布时间:2020-05-25 03:26:52 所属栏目:PHP 来源:互联网
导读:1、创建事务的结构SqlConnection sqlConnection = new SqlConnection();...初始化连接// 开启事务SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();// 将事务应用于CommandSqlCommand sqlCommand = new SqlCommand();sqlCommand.Connection

1、创建事务的结构

SqlConnection sqlConnection = SqlTransaction sqlTransaction = SqlCommand sqlCommand = ==

2、简单例子

= = System.Data.SqlClient.SqlConnection(= ==(DataRow dr = ].Value = Convert.ToInt32(dr[].Value = dr[

另外一个例子,稍微复杂一点,注意每次执行的时候要清除上一次的参数

identity = = SqlConnection(ConfigurationManager.ConnectionStrings[==== </span><span style="color: #0000ff;"&gt;try</span><span style="color: #000000;"&gt; { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;1写入问卷</span> command.CommandText = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;Questionnaire_ADD</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;; SqlParameter[] parms </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; SqlParameter[] { </span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@ID</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,SqlDbType.Int),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@QuestionnaireTitle</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,SqlDbType.NVarChar) }; parms[</span><span style="color: #800080;"&gt;0</span>].Value =<span style="color: #000000;"&gt; questionnaire.ID; parms[</span><span style="color: #800080;"&gt;1</span>].Value =<span style="color: #000000;"&gt; questionnaire.QuestionnaireTitle; command.Parameters.AddRange(parms); </span><span style="color: #0000ff;"&gt;int</span> questionnaireID =<span style="color: #000000;"&gt; Convert.ToInt32(command.ExecuteScalar()); identity </span>=<span style="color: #000000;"&gt; questionnaireID; </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;2写入题目</span> <span style="color: #0000ff;"&gt;for</span>(<span style="color: #0000ff;"&gt;int</span> i = <span style="color: #800080;"&gt;0</span>; i < questionnaire.Questions.Count; i++<span style="color: #000000;"&gt;) { command.CommandText </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;Question_ADD</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;; command.Parameters.Clear(); parms </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; SqlParameter[] { </span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@ID</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.ID),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@QuestionTitle</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.Questions[i].QuestionTitle),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@Sequence</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.Questions[i].Sequence),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@QuestionTypeID</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.Questions[i].QuestionTypeID),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@QuestionnaireID</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaireID)}; command.Parameters.AddRange(parms); </span><span style="color: #0000ff;"&gt;int</span> questionID =<span style="color: #000000;"&gt; Convert.ToInt32(command.ExecuteScalar()); identity </span>=<span style="color: #000000;"&gt; questionID; </span><span style="color: #0000ff;"&gt;if</span>((questionnaire.Questions[i].QuestionType == QuestionType.singlechoice) || (questionnaire.Questions[i].QuestionType ==<span style="color: #000000;"&gt; QuestionType.multiplechoice)) { </span><span style="color: #0000ff;"&gt;for</span>(<span style="color: #0000ff;"&gt;int</span> j = <span style="color: #800080;"&gt;0</span>; j < questionnaire.Questions[i].ChoiceItems.Count; j++<span style="color: #000000;"&gt;) { command.CommandText </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;ChoiceItem_ADD</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;; command.Parameters.Clear(); parms </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; SqlParameter[]{ </span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@ID</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.Questions[i].ChoiceItems[j].ID),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@ItemContent</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.Questions[i].ChoiceItems[j].ItemContent),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@Options</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionnaire.Questions[i].ChoiceItems[j].Option),</span><span style="color: #0000ff;"&gt;new</span> SqlParameter(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;@QuestionID</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,questionID)}; command.Parameters.AddRange(parms); identity </span>=<span style="color: #000000;"&gt; Convert.ToInt32(command.ExecuteScalar()); } } } transaction.Commit(); } </span><span style="color: #0000ff;"&gt;catch</span><span style="color: #000000;"&gt;(Exception ex) { transaction.Rollback(); </span><span style="color: #0000ff;"&gt;throw</span><span style="color: #000000;"&gt;; } </span><span style="color: #0000ff;"&gt;finally</span><span style="color: #000000;"&gt; { connection.Close(); transaction.Dispose(); connection.Dispose(); } </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; identity; }</span></pre>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读