欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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();
}
}
}