日期:2014-05-17 浏览次数:20588 次
USE [OperationData] GO /****** Object: StoredProcedure [dbo].[Pro_UserMax] Script Date: 09/28/2012 09:23:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[Pro_UserMax] @maxNum int output AS begin select @maxNum=MAX(Convert(int,PerNum)) from Sys_user end
try { SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@maxNum",ParameterDirection.Output) }; Convert.ToInt32(SQLHelper.ExecuteNonQuery(SQLHelper.sqlConnectionString, CommandType.StoredProcedure, "Pro_UserMax", parm)); int Maxid = Convert.ToInt32(parm[0].Value);//现在maxid的值老是2 return Maxid; } catch(Exception ex) { throw ex; } //这是sqlhelp中的方法 public static int ExecuteNonQuery(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); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } }
SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@maxNum",ParameterDirection.Output) };//这里再指定下数据类型 SQLHelper.ExecuteNonQuery(SQLHelper.sqlConnectionString, CommandType.StoredProcedure, "Pro_UserMax", parm); int Maxid = Convert.ToInt32(parm[0].Value); return Maxid;
------解决方案--------------------
在存储过程中加
return @maxNum
然后调用方法中加
"return",ParameterDirection.ReturnValue
把int Maxid = Convert.ToInt32(parm[0].Value改成
cmd.Parameters["return"].Value;