问题:说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息
异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 181: if(HasReturn)
行 182: {
行 183: result = (int)_command.Parameters["@RETURN_VALUE"].Value ;
行 184: }
行 185:
源文件: e:\docadmin1.1_51aspx\dal\dataaccess.cs 行: 183
dataaccess.cs:
private static int ExecuteNonQuery_in(string commandText,bool HasReturn, params SqlParameter[] commandParameters)
{
int result = -1 ;
//创建并打开数据连接
if (_connection.State != ConnectionState.Open)
{
_connection.Open();
}
_command.Connection = _connection ;
PrepareCommand(commandText, commandParameters); //调用执行函数
//执行命令
try
{
result = _command.ExecuteNonQuery();
}
catch(Exception e)
{
//执行失败
//从异常中获取失败信息
string msg = e.Message ;
//调用写日志方法
Trace.Write("ExecuteNonQuery","Error",commandText+"|"+msg) ;
}
if(HasReturn)
{
result = (int)_command.Parameters["@RETURN_VALUE"].Value ;
}
//清除参数
_command.Parameters.Clear(); //执行完毕后关闭连接
_connection.Close() ; return result ;
}
异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 181: if(HasReturn)
行 182: {
行 183: result = (int)_command.Parameters["@RETURN_VALUE"].Value ;
行 184: }
行 185:
源文件: e:\docadmin1.1_51aspx\dal\dataaccess.cs 行: 183
dataaccess.cs:
private static int ExecuteNonQuery_in(string commandText,bool HasReturn, params SqlParameter[] commandParameters)
{
int result = -1 ;
//创建并打开数据连接
if (_connection.State != ConnectionState.Open)
{
_connection.Open();
}
_command.Connection = _connection ;
PrepareCommand(commandText, commandParameters); //调用执行函数
//执行命令
try
{
result = _command.ExecuteNonQuery();
}
catch(Exception e)
{
//执行失败
//从异常中获取失败信息
string msg = e.Message ;
//调用写日志方法
Trace.Write("ExecuteNonQuery","Error",commandText+"|"+msg) ;
}
if(HasReturn)
{
result = (int)_command.Parameters["@RETURN_VALUE"].Value ;
}
//清除参数
_command.Parameters.Clear(); //执行完毕后关闭连接
_connection.Close() ; return result ;
}
{
result = (int)_command.Parameters["@RETURN_VALUE"].Value ;
} //清除参数
_command.Parameters.Clear(); //执行完毕后关闭连接
_connection.Close() ; return result ;
}这些代码为什么要放在catch块内啊?明显应该存在于finial块内的阿;
获取返回参数,好像是先要定义好这个参数是返回参数,然后执行完毕之后才能取值。。
SqlParameter p = new SqlParameters();
然后设置 p 为返回参数。
然后将 p 加入 Parameters 里面
然后执行
最后取值,才能取到