我不知道在C#中怎么用代码调用事务,请高手给我指点指点,谢谢啦!!!!!
解决方案 »
- c# 键盘钩子执行两次
- 在一台机器上,多个.net可以共存吗?
- 关于IIS的问题。
- excel自定义菜单事件问题。
- 重新运行程序,所有数据库数据全部清空?!
- 如何序列化窗体中的所有控件。在线等。散分
- C#里怎样读正被别的程序打开的文件?
- DataGrid中添加DateTimePicke控件,该怎么添加呢?用C#~~~~~~~~~
- 关于ComboBox的怪现象
- DataGrid主从表绑定的问题
- 由于泛型函数xx预先不知道T是什么类型的对象,所以调用xx时,当T为string对象时,像这样的句子“y=a.Length”就不能在泛型函数体中使用,有什么通用办法解决吗?请看代码:
- 如何在一个固定时间来执行程序?
一、用system.transactions
二、用存储过程,把事务写在存储过程中,在代码中传递参数,调用存储过程。
三、代码:public static int ExecuteSqlTran(List<String> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch
{
tx.Rollback();
return 0;
}
}
}
using (SqlConnection conn = new SqlConnection(connectionString)) 这一句是什么意思啊?为什么要用using啊?我不知道,请再给我说说,谢啦!!!!
{
...................
}
这是using的另外一种用法:新建conn对象,当出现异常或者执行结束后,销毁conn对象。
这三种方法是三种独立的方法。
List<string> sqlList = new List<string>();
sqlList.Add("SQL语句一")
sqlList.Add("SQL语句二")
sqlList.Add("SQL语句三")
sqlList.Add("SQL语句四")
.....................ExecuteSqlTran(sqlList)。
这是其中的一种用法,还有,比较简单的就是用:system.transactions,具体用法你在网上搜搜。