日期:2014-05-17 浏览次数:20473 次
use OperationData go create procedure Pro_UserMax @maxNum int output as begin select @maxNum=MAX(Convert(int,PerNum)) from Sys_user if @maxNum is null --@maxNum为null的话就返回10001 return 10001 else --否则返回最大值 return @maxNum end
//获取最大值 public static int ExecuteMaxQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); cmd.Parameters.Add("@maxNum", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); //这里value的值怎么老是null 难道存储过程 根本就没执行 if..else..语句? int val = Convert.ToInt32(cmd.Parameters["@maxNum"].Value); //现在运行到这里就报错了 这里value的值为null 所以转换报错 return val; } }
use OperationData go create procedure Pro_UserMax ( @maxNum int output ) as begin select @maxNum=MAX(Convert(int,PerNum)) from Sys_user if @maxNum is null --@maxNum为null的话就返回10001 select @maxNum = 10001 end
------解决方案--------------------
create procedure dbo.Pro_UserMax @maxNum int output AS SET @maxNum=ISNULL((SELECT MAX(Convert(int,PerNum)) FROM Sys_user),10001);