sql事务应用积累
程序员文章站
2023-08-23 19:19:24
复制代码 代码如下:/// /// 批量执行sql语句 /// ///
/// <summary>
/// 批量执行sql语句
/// </summary>
/// <param name="sqlstr">sql语句数组</param>
/// <param name="param">sql参数对象数组</param>
/// <returns></returns>
public static int32 executesqls(string[] sqlstr, list<sqlparameter[]> param)
{
string connstr = getsqlconnection();
using (sqlconnection conn = new sqlconnection(connstr))
{
sqlcommand cmd = new sqlcommand();
sqltransaction tran = null;
cmd.transaction = tran;
try
{
conn.open();
tran = conn.begintransaction();
cmd.connection = conn;
cmd.transaction = tran;
int32 count = sqlstr.length;
for (int32 i = 0; i < count; i++)
{
cmd.commandtext = sqlstr[i];
cmd.parameters.addrange(param[i]);
cmd.executenonquery();
}
tran.commit();
return 1;
}
catch
{
tran.rollback();
return 0;
}
finally
{
cmd.dispose();
conn.close();
}
}
}
复制代码 代码如下:
/// <summary>
/// 批量执行sql语句
/// </summary>
/// <param name="sqlstr">sql语句数组</param>
/// <param name="param">sql参数对象数组</param>
/// <returns></returns>
public static int32 executesqls(string[] sqlstr, list<sqlparameter[]> param)
{
string connstr = getsqlconnection();
using (sqlconnection conn = new sqlconnection(connstr))
{
sqlcommand cmd = new sqlcommand();
sqltransaction tran = null;
cmd.transaction = tran;
try
{
conn.open();
tran = conn.begintransaction();
cmd.connection = conn;
cmd.transaction = tran;
int32 count = sqlstr.length;
for (int32 i = 0; i < count; i++)
{
cmd.commandtext = sqlstr[i];
cmd.parameters.addrange(param[i]);
cmd.executenonquery();
}
tran.commit();
return 1;
}
catch
{
tran.rollback();
return 0;
}
finally
{
cmd.dispose();
conn.close();
}
}
}
下一篇: Photoshop入门教程:扭曲变幻线
推荐阅读
-
Sql Server中的事务介绍
-
SQL Server误区30日谈 第15天 CheckPoint只会将已提交的事务写入磁盘
-
SQL Server误区30日谈 第26天 SQL Server中存在真正的“事务嵌套”
-
SQL2000 事务回滚问题探讨
-
SQL Server控制语句的基本应用
-
SQL Server移除事务日志后sys.master_files依然存在记录问题
-
html5 Web SQL Database 之事务处理函数transaction与executeSQL解析
-
html5 Web SQL Database 之事务处理函数transaction与executeSQL解析
-
SQL 截取字符串应用代码
-
sql事务的使用及其技巧整理