.NET C#事务处理

具体怎么实现呢?请发一下代码。有几种事务?... 具体怎么实现呢?请发一下代码。

有几种事务?
展开
 我来答
amwih001
2010-09-09 · TA获得超过2.3万个赞
知道大有可为答主
回答量:1529
采纳率:100%
帮助的人:664万
展开全部
SqlConnection con = new SqlConnection(ConnectionDb.conStr);//获取数据库连接
con.Open();//打开连接
SqlTransaction sqltra = con.BeginTransaction();//开始事务
SqlCommand cmd = new SqlCommand();//实例化
cmd.Connection = con;//获取数据连接
cmd.Transaction = sqltra;//,在执行SQL时,
try
{
string sql = "insert into OA_MEETING(meeting_id,meeting_name,meeting_title,start_time,end_time,announce_time,explain,reg_emp_id,emc_emp_id,par_emp_id,rep_emp_id,participate,MEETING_TYPE,PROJ_ID,WORK_ID)values('" + id + "',N'" + meetname + "',N'" + meettitle + "','" + starttime + "','" + endtime + "',N'" + djtime + "',N'" + meetinfo + "','" + empid + "','" + meetperson + "','" + lname + "','" + writer + "','" + ccname + "','" + hidsort.Value + "','" + proid + "','" + workid + "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

//string meetid = id;
int size = 0;
HttpFileCollection files = HttpContext.Current.Request.Files;
//遍历上传文件窗体所有Html控件
foreach (string NameId in Request.Form)
{
//找到控件ID名前四位为"txt_"的文件说明文本框
if (NameId.Substring(0, 4) == "ttx_")
{
//获取文本说明
if (Request.Form[NameId] != "")
{
string Explain = Request.Form[NameId];//获取txt文本说明

HttpPostedFile pstfile = files["fil_" + NameId.Substring(4)];
string fileName = "";
string fileExtension = "";
fileName = System.IO.Path.GetFileName(pstfile.FileName);//上传的文件全名
fileExtension = System.IO.Path.GetExtension(fileName);//扩展名
string NewName = fileName.Remove(fileName.LastIndexOf("."));//去掉后缀名的文件名
string sql1 = "select isnull(max(app_id),0) from OA_MEETING_APPENDIX";

int idd = Convert.ToInt32(objConnction.getString(sql1)) + 1;
ua.Folder_Exists("Meeting");
string filepath = "..\\AtthFiles\\Meeting\\Meeting_" + idd.ToString() + fileExtension;
string sqlstr1 = "insert into OA_MEETING_APPENDIX(APP_ID,MEETING_ID,EXPLAIN,APP_PATH) values('" + idd + "','" + id + "',N'" + Explain + "',N'" + filepath + "')";
cmd.CommandText = sqlstr1;

int num = cmd.ExecuteNonQuery();
if (num > 0)
{
pstfile.SaveAs(Server.MapPath("..\\AtthFiles\\Meeting\\Meeting_" + idd.ToString() + fileExtension));
size = pstfile.ContentLength / 1024;
objDocument.InsertDoc(5, idd, Explain, "", "", DateTime.Now.ToString("yyyy-MM-dd"), Convert.ToInt32(HempID.Value), filepath, size, hidwork.Value, hidpro.Value);
}
}
}
}
sqltra.Commit();
}catch(Exception ex)
{
sqltra.Rollback();
}

参考资料: 随手一写

bjddd192
2010-09-16 · TA获得超过416个赞
知道小有建树答主
回答量:496
采纳率:0%
帮助的人:432万
展开全部
只有一种事务,但是有几种隔离级别,可参见IsolationLevel枚举
给个例子你,例子是使用的DB2数据库,你可以改为其他的
private void button10_Click(object sender, EventArgs e)
{
DbProviderFactory factory = DbProviderFactories.GetFactory(@"IBM.Data.DB2");

DbConnection con = factory.CreateConnection();
con.ConnectionString =
@"Server=192.20.30.167:50000;Database=DEVP;UID=db2;PWD=db2;
CurrentSchema=TK;Connection Lifetime=60;Connection Reset=false;
Max Pool Size=5;Min Pool Size=3;Pooling=True;Connect Timeout=15";

DbCommand cmd = factory.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbtickettype";
cmd.Connection = con;
cmd.CommandTimeout = 300;

DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = cmd;

con.Open();
DbTransaction tran = con.BeginTransaction(IsolationLevel.ReadCommitted);
da.SelectCommand.Transaction = tran;//在连接起事务后,必须指定命令的事务
try
{
DataSet ds = new DataSet();
da.Fill(ds);

ds.Tables[0].Rows[0]["TICKETNAME"] = "面部走引组()";
cmd = factory.CreateCommand();
cmd.CommandText = "update tk.TBTICKETTYPE set ticketname = '面部走引组(zzz)' where tickettype = 19";
cmd.Connection = con;
cmd.CommandTimeout = 300;
da.UpdateCommand = cmd;
da.UpdateCommand.Transaction = tran;
int a = da.Update(ds);//只有修改状态的才执行更新语句

DbConnection con1 = factory.CreateConnection();
con1.ConnectionString =
@"Server=172.20.30.107:50000;Database=BL_DEVP;UID=db2inst1;PWD=db2;
CurrentSchema=TK;Connection Lifetime=60;Connection Reset=false;
Max Pool Size=5;Min Pool Size=3;Pooling=True;Connect Timeout=15";

DbCommand cmd1 = factory.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select * from tbtickettype";
cmd1.Connection = con1;
cmd1.CommandTimeout = 300;
da = factory.CreateDataAdapter();
da.SelectCommand = cmd1;
con1.Open();
DbTransaction tran1 = con1.BeginTransaction(IsolationLevel.ReadUncommitted);//区别就是可以脏读(及能读出事务过程中已经修改但未提交的数据)
da.SelectCommand.Transaction = tran1;
DataSet ds1 = new DataSet();
da.Fill(ds1);
tran1.Commit();
con1.Close();
tran.Commit(); //在提交前如果进行select * from tk.TBTICKETTYPE with ur就能读出尚未提交的更新,实际上就是ReadUnCommitted的效果
MessageBox.Show("OK");
}
catch
{
tran.Rollback();
}
finally
{
tran.Dispose();
con.Close();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Linju1020
2010-09-10
知道答主
回答量:12
采纳率:0%
帮助的人:5.9万
展开全部
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
int indentity = 0;
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Key.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
foreach (SqlParameter q in cmdParms)
{
if (q.Direction == ParameterDirection.InputOutput)
{
q.Value = indentity;
}
}
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
foreach (SqlParameter q in cmdParms)
{
if (q.Direction == ParameterDirection.Output)
{
indentity = Convert.ToInt32(q.Value);
}
}
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
http://www.xinqi8.com/c_mystery/Default.aspx
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式