数据库操作类有如下方法
public int RunCommand(string str,string ConnStr)
{
int count = -1;
SqlConnection conn = GetConn(ConnStr);
try
{
SqlCommand comm = new SqlCommand(str);
comm.Connection = conn;
conn.Open();
count = comm.ExecuteNonQuery();//执行sql语句
}
catch (Exception ex)
{
// HttpContext.Current.Response.Write(ex.Message);
count = -1;
}
finally
{
CloseConn(conn);
}
return count;
}
在cs中调用此方法向数据库插入数据,而插入的数据违反了约束,抛出错误,而这个错误处理只在数据操作类中进行了,而在Cs程序中会继续运行程序,导致程序出错,或不是想要的结果,我应该如何做
public int RunCommand(string str,string ConnStr)
{
int count = -1;
SqlConnection conn = GetConn(ConnStr);
try
{
SqlCommand comm = new SqlCommand(str);
comm.Connection = conn;
conn.Open();
count = comm.ExecuteNonQuery();//执行sql语句
}
catch (Exception ex)
{
// HttpContext.Current.Response.Write(ex.Message);
count = -1;
}
finally
{
CloseConn(conn);
}
return count;
}
在cs中调用此方法向数据库插入数据,而插入的数据违反了约束,抛出错误,而这个错误处理只在数据操作类中进行了,而在Cs程序中会继续运行程序,导致程序出错,或不是想要的结果,我应该如何做
你在cs代码中if( RunCommand("","") >-1){//你的代码}
要这样的?返回-1不执行后续代码就这样写啊,后续代码写在if语句中就行了