C#的一段插入数据库的代码,我有个存SQL语句的LIST,用for循环一条条的执行sql
publicboolExecDataBySqls(List<string>strSqls){boolbooIsSucceed;if(m_Conn.State==Conne...
public bool ExecDataBySqls(List<string> strSqls)
{
bool booIsSucceed;
if (m_Conn.State == ConnectionState.Closed)
{
m_Conn.Open();
}
MySqlTransaction sqlTran = m_Conn.BeginTransaction();
string item=String.Empty;
try
{
m_Cmd.Transaction = sqlTran;
for(int i=0;i<strSqls.Count;i++)
{
item = strSqls[i].ToString();
m_Cmd.CommandType = CommandType.Text;
m_Cmd.CommandText = item;
//m_Cmd.CommandText = strSqls.ToString();
m_Cmd.ExecuteNonQuery();
}
sqlTran.Commit();
booIsSucceed = true; //表示提交数据库成功
}
catch
{
Log.outLog("List的数据插入数据库失败","插入失败" + item);
sqlTran.Rollback();
booIsSucceed = false; //表示提交数据库失败!
}
finally
{
// m_Conn.Close();
strSqls.Clear();
}
return booIsSucceed;
}
现在请问如果我其中有一条语句出错,只是把这条语句给跳过,而不是直接报错 展开
{
bool booIsSucceed;
if (m_Conn.State == ConnectionState.Closed)
{
m_Conn.Open();
}
MySqlTransaction sqlTran = m_Conn.BeginTransaction();
string item=String.Empty;
try
{
m_Cmd.Transaction = sqlTran;
for(int i=0;i<strSqls.Count;i++)
{
item = strSqls[i].ToString();
m_Cmd.CommandType = CommandType.Text;
m_Cmd.CommandText = item;
//m_Cmd.CommandText = strSqls.ToString();
m_Cmd.ExecuteNonQuery();
}
sqlTran.Commit();
booIsSucceed = true; //表示提交数据库成功
}
catch
{
Log.outLog("List的数据插入数据库失败","插入失败" + item);
sqlTran.Rollback();
booIsSucceed = false; //表示提交数据库失败!
}
finally
{
// m_Conn.Close();
strSqls.Clear();
}
return booIsSucceed;
}
现在请问如果我其中有一条语句出错,只是把这条语句给跳过,而不是直接报错 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询