类为:
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using symptom_msg;namespace symptom_blog
{
/// <summary>
/// 存储过程操作类
/// </summary>
public class Symptom_StoredPro_Operator
{ /// <summary>
/// SqlCommand对象
/// </summary>
public SqlCommand myCommand; /// <summary>
/// SqlParameter对象
/// </summary>
public SqlParameter myParameter; /// <summary>
/// SqlDataReader对象
/// </summary>
public SqlDataReader myReader; /// <summary>
/// 数据库对象
/// </summary>
public Symptom_DataBase_Operator Data; /// <summary>
/// 存储过程名
/// </summary>
private string sp_name;
public string ProcedureName { get

return this.sp_name; 
} set

this.sp_name = value; 
} } public Symptom_StoredPro_Operator() : this("") { } public Symptom_StoredPro_Operator(string sp_name) { this.ProcedureName = sp_name; } public SqlResult Call(params object[] parameters)
{
// SqlResult是自己定义的用于保存结果数据集、返回值、传出参数集的类
SqlResult result = new SqlResult();
Data = new Symptom_DataBase_Operator();
myCommand = new SqlCommand(this.ProcedureName,Data.C_Temp);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
Data.Open_Sql();
// 获得和创建存储过程的参数,并且设置好值
GetProcedureParameter(parameters);
myAdapter.Fill(result.dataSet, "Table");
// 获得存储过程的传出参数值和名字对,保存在一个Hashtable中
GetOutputValue(result);
myCommand.Dispose();
Data.Close_Sql_Conn(); return result;
} /// <summary>
/// 创建存储过程的各种参数
/// 说明:此处仅需提供存储过程名即可从数据库中检索该过程的各种属性
/// 具体说明见 http://www.uppic.com/content.asp?picid=5858
/// </summary>
/// <param name="parameters"></param>
private void GetProcedureParameter(params object[] parameters) {
SqlCommand myCommand2 = new SqlCommand();
myCommand2.Connection = this.Data.C_Temp;
myCommand2.CommandText = "select * from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME='" +this.ProcedureName+ "' order by ORDINAL_POSITION";
SqlDataReader reader = null;
try 
{
// 创建返回参数
reader = myCommand2.ExecuteReader();
myParameter = new SqlParameter();
myParameter.ParameterName = "@Value";
myParameter.SqlDbType = SqlDbType.Int;
myParameter.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(myParameter);
int i = 0;
// 创建各个参数,在这个地方可以自动的创建SqlParameter的类型,值,方向等属性
while(reader.Read())
{
myParameter = new SqlParameter();
myParameter.ParameterName = reader["PARAMETER_NAME"].ToString();
myParameter.Direction = reader["PARAMETER_MODE"].ToString()=="IN"?ParameterDirection.Input:ParameterDirection.Output;
switch(reader["DATA_TYPE"].ToString())
{
case "bit" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (bool)parameters[i];
myParameter.SqlDbType = SqlDbType.Bit;
break; case "bigint" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (int)parameters[i];
myParameter.SqlDbType = SqlDbType.BigInt;
break; case "int" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (int)parameters[i];
myParameter.SqlDbType = SqlDbType.Int;
break; case "decimal" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (double)parameters[i];
myParameter.SqlDbType = SqlDbType.Decimal;
myParameter.Precision = (byte)reader["NUMERIC_PRECISION"];
myParameter.Scale = (byte)reader["NUMERIC_SCALE"];
break; case "nvarchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = (int)reader["CHARACTER_MAXIMUM_LENGTH"];
myParameter.SqlDbType = SqlDbType.NVarChar;
break; case "varchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = (int)reader["CHARACTER_MAXIMUM_LENGTH"];
myParameter.SqlDbType = SqlDbType.VarChar;
break; case "nchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = (int)reader["CHARACTER_MAXIMUM_LENGTH"];
myParameter.SqlDbType = SqlDbType.NChar;
break; case "char" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = (int)reader["CHARACTER_MAXIMUM_LENGTH"];
myParameter.SqlDbType = SqlDbType.Char;
break; case "ntext" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.NText;
break; case "text" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.Text;
break; case "datetime" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (DateTime)parameters[i];
myParameter.SqlDbType = SqlDbType.DateTime;
break; case "smalldatetime" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (DateTime)parameters[i];
myParameter.SqlDbType = SqlDbType.DateTime;
break; case "image" :
if(myParameter.Direction == ParameterDirection.Input)
{
HttpPostedFile PostedFile = (HttpPostedFile)parameters[i];
Byte[] FileByteArray = new Byte[PostedFile.ContentLength];
Stream StreamObject = PostedFile.InputStream;
StreamObject.Read(FileByteArray,0,PostedFile.ContentLength);
myParameter.Value = FileByteArray; }
myParameter.SqlDbType = SqlDbType.Image;
break; case "uniqueidentifier" :
//myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.UniqueIdentifier;
break;
default : break;
}
i++;
myCommand.Parameters.Add(myParameter);
}
} catch(Exception e)
{
throw e;
} finally
{
if(reader!=null) reader.Close();
myCommand2.Dispose();
} } private void GetOutputValue(SqlResult result)
{
result.Value = (int)myCommand.Parameters["@Value"].Value;
foreach(SqlParameter parameter in myCommand.Parameters)
{
if(parameter.Direction == ParameterDirection.Output)
{
result.Output.Add(parameter.ParameterName, parameter.Value);
}
} } }

/// <summary>
///  存储过程的返回值纪录类
///  DataSet : 表示返回的表
///  Output  : 存储过程的输出参数
///  Value   : 存储过程的返回值
/// </summary>
public class SqlResult { public int Value;
public Hashtable Output;
public DataSet dataSet; public SqlResult()
{
Value = 0;
Output = new Hashtable();
dataSet = new DataSet();
}
}}报错信息:
Fill: SelectCommand.Connection 属性尚未初始化。 源错误: 
行 87:  // 获得和创建存储过程的参数,并且设置好值
行 88:  GetProcedureParameter(parameters);
行 89:  myAdapter.Fill(result.dataSet, "Table");
行 90:  // 获得存储过程的传出参数值和名字对,保存在一个Hashtable中
行 91:  GetOutputValue(result);
 

解决方案 »

  1.   

    myCommand = new SqlCommand(this.ProcedureName,Data.C_Temp);
    myCommand.CommandType = CommandType.StoredProcedure;
                               myCommand.Connection =.....;连接对象
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
    Data.Open_Sql();
    // 获得和创建存储过程的参数,并且设置好值
    GetProcedureParameter(parameters);
      

  2.   

    Symptom_StoredPro_Operator procedure=new Symptom_StoredPro_Operator();
    SqlResult result;
    procedure.ProcedureName="Blog_Login";
    result=procedure.Call(user,pwd);如何取回返回值》》》?????
      

  3.   

    如果你返回的是单个值那么你可以在command添加一个direction=output的parameter