sql事务应用积累
程序员文章站
2022-06-04 22:54:05
复制代码 代码如下:/// /// 批量执行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();
}
}
}
上一篇: Python实现处理逆波兰表达式示例
下一篇: Git如何删除历史记录中的大文件详解