日期:2014-05-18 浏览次数:20583 次
public SqlParameter CreateInParam(string ParamName, SqlDbType DbType, int Size, object Value) { return CreateParam(ParamName, DbType, Size, ParameterDirection.Input, Value); } public SqlParameter CreateParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) { SqlParameter param; ///当参数大小为0时,不使用该参数大小值 if (Size > 0) { param = new SqlParameter(ParamName, DbType, Size); } else { ///当参数大小为0时,不使用该参数大小值 param = new SqlParameter(ParamName, DbType); } ///创建输出类型的参数 param.Direction = Direction; if (!(Direction == ParameterDirection.Output && Value == null)) { param.Value = Value; } ///返回创建的参数 return param; } //得到cmd public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader) { ///创建Command SqlCommand cmd = CreateProcCommand(procName, prams); ///读取数据 dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } private SqlCommand CreateProcCommand(string procName, SqlParameter[] prams) { ///打开数据库连接 Open(); ///设置Command SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.StoredProcedure; ///添加把存储过程的参数 if (prams != null) { foreach (SqlParameter parameter in prams) { cmd.Parameters.Add(parameter); } } ///添加返回参数ReturnValue cmd.Parameters.Add( new SqlParameter(RETURNVALUE, SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); ///返回创建的SqlCommand对象 return cmd; }
public SqlDataReader GetAdminID(string sAdminName, string sPassword) { SQLHelper sqlHelp = new SQLHelper(); SqlParameter[] prams = { sqlHelp.CreateInParam("@AdminName", SqlDbType.VarChar, 200, sAdminName), sqlHelp.CreateInParam("@Password", SqlDbType.VarChar, 50, sPassword) }; SqlDataReader dr; sqlHelp.RunProc("Pr_GetAdminID", prams, out dr); return dr; }