我正在创建一个分离数据库的程序。
我如何检查存储过程是否已运行而没有错误,以便我可以向用户确认它已完全完成。
我try catch
在执行周围使用 a ,但我不是 100% 确定这会返回所有内部错误?
我担心的是,如果我写:
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("it has finished");
}
catch (exception ex)
{
ex.ToString();
}
不可靠
我的命令:
return "sp_detach_db '" + dbType[db] + "', 'true'";
您的原始代码很好(try/catch)...您传入的字符串是您尝试执行的操作的棘手部分。我会看这个答案,找出返回 0 或 1 的命令........如果返回值为 1,那么您将抛出一个新异常。
根据文档:您在 RETURN_CODE 之后。
Return Code Values
0 (success) or 1 (failure)
Result Sets
None
伪c#代码
using (SqlConnection conn = new SqlConnection("MyString"))
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = string.Format("sp_detach_db '{0}', 'true'", myDatabaseName); /* where myDatabaseName is a string */
cmd.CommandType = CommandType.Text;
var returnParameter = cmd.Parameters.Add("@ReturnVal", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
object result = returnParameter.Value;
int resultInt = Convert.ToInt32(result);
if( 0 != resultInt )
{ throw new ArgumentOutOfRangeException("detach failed");}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句