复制代码 代码如下:
/// <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();
}
}
}
标签:
sql,事务应用
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“sql事务应用积累”评论...