zoukankan
html css js c++ java
在程序中使用事务,处理无外键关系的表与表间的操作。
程序中使用事务,处理无外键关系的表与表间的操作
//
创建事务连接对象
SqlConnection myConn
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
conString
"
]);
try
{
myConn.Open();
//
打开连接
}
catch
(Exception ex)
{
labError.Text
=
"
提示信息:连接数据库失败,请查看网络!
"
+
ex.Message;
return
;
}
SqlTransaction myTrans
=
myConn.BeginTransaction();
//
主表事务
//
将连接对象和事务对象传入用于处理
iSuccess
=
Test_Add(myConn,myTrans,iState,txtTopic.Text.Trim(),txtCont.Text.Trim());
if
(iSuccess
>
0
)
{
int
iNum
=
0
;
//
将附件存入数据库
for
(
int
i
=
1
;i
<
local.Length;i
++
)
{
//
截取\User文件夹下的路径local[i]附件路径,多附件。
local[i]
=
local[i].Substring(local[i].IndexOf(
"
\\User
"
));
iNum
=
Attachment(myConn,myTrans,iSuccess,local[i]);
if
(iNum
<
1
)
{
//
myTrans.Rollback();
myConn.Close();
myConn.Dispose();
labError.Text
=
"
提示信息:附件加载失败,请查看网络!
"
;
return
;
}
}
iNum
=
0
;
//
将接收邮件用户信息存入数据库
for
(
int
i
=
0
;i
<
ary_Email.Count;i
++
)
{
//
添加邮件接收人的联系地址与用户名和议案调查的答题地址,因为不是议案调查所以答题地址为空
iNum
=
AddEmail(myConn,myTrans,iSuccess,ary_Name[i].ToString(),
""
,ary_Email[i].ToString());
if
(iNum
<
1
)
{
myTrans.Rollback();
myConn.Close();
myConn.Dispose();
labError.Text
=
"
提示信息:接收邮件用户信息加载失败,请查看网络!
"
;
return
;
}
}
myTrans.Commit();
//
事务结束
myConn.Close();
//
关闭连接
myConn.Dispose();
iNum
=
0
;
//
获取配置文件中的公司股票代码号
string
tmpCode
=
System.Configuration.ConfigurationSettings.AppSettings[
"
Code
"
].ToString().Trim();
//
写入分发中心审核信息表中
iNum
=
AddAuditing(tmpCode,iSuccess,txtTopic.Text.Trim(),txtCont.Text.Trim(),iCount,strUserName);
if
(iNum
<
1
)
{
labError.Text
=
"
提示信息:写入失败,请查看网络!
"
;
return
;
}
labError.Text
=
"
提示信息:邮件已经发送成功!
"
;
}
else
{
myTrans.Rollback();
myConn.Close();
myConn.Dispose();
labError.Text
=
"
提示信息:发送失败,网络繁忙!
"
;
}
//
以下为处理函数*************
public
static
int
Attachment(SqlConnection myConnection,SqlTransaction myTrans,
int
iSendLogID,
string
strAttachment)
{
//
Create Instance of Connection and Command Object
//
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
SqlCommand myCommand
=
new
SqlCommand(
"
MailAttachment_Add
"
, myConnection);
myCommand.Transaction
=
myTrans;
//
Mark the Command as a SPROC
myCommand.CommandType
=
CommandType.StoredProcedure;
//
Add Parameters to SPROC
SqlParameter SPSuccess
=
new
SqlParameter(
"
@iSuccess
"
, SqlDbType.Int,
4
);
SPSuccess.Direction
=
ParameterDirection.Output;
myCommand.Parameters.Add(SPSuccess);
SqlParameter SPSendLogID
=
new
SqlParameter(
"
@iSendLogID
"
, SqlDbType.Int);
SPSendLogID.Value
=
iSendLogID;
myCommand.Parameters.Add(SPSendLogID);
SqlParameter SPAttachment
=
new
SqlParameter(
"
@Attachment
"
, SqlDbType.VarChar,
200
);
SPAttachment.Value
=
strAttachment;
myCommand.Parameters.Add(SPAttachment);
//
Open the database connection and execute the command
int
iMailAttachmentID
=
0
;
try
{
//
myConnection.Open();
myCommand.ExecuteNonQuery();
iMailAttachmentID
=
(
int
)SPSuccess.Value;
//
myConnection.Close();
}
catch
(Exception ex)
{
Console.Write(ex.Message);
return
-
1
;
}
return
iMailAttachmentID;
}
查看全文
相关阅读:
Windows快捷键大全
Windows快捷键大全
长尾理论
长尾理论
长尾理论
Windows快捷键大全
Windows快捷键大全
Windows快捷键大全
长尾理论
e2eCapWDM Video Capture 服务启动失败解决方法
原文地址:https://www.cnblogs.com/hanguoji/p/563843.html
最新文章
关于php 时间的处理
Yii tinymce实现语法高亮
php中static关键字在类中的使用
PHP常用工具
checkbox的多选与反选 jQuery 实现
数据库修改了密码Mantis 连接
服务器状态代码含义
配置drupal的clean URL
PHP类继承
PHP short_open_tag 支持等号简短缩写
热门文章
精通C++的道路
ACM/ICPC 2011 AsiaAmritapuri Site / E Distinct Primes(求数的素因子)
POJ 2313 Sequence (贪心)
USACO / Mother's Milk (DFS)
Codeforces Round #124 (Div. 2) / C. Lexicographically Maximum Subsequence
Trie树(留待自己总结)
POJ 1405 Heritage(贪心)
N!分解素因子及若干问题
一个奇妙、离奇的算法题
ACM/ICPC 2011 AsiaAmritapuri Site / B Save the Students!(判断点在三角形中)
Copyright © 2011-2022 走看看