C#中调用存储过程(带返回参数 And 无返回参数)
/// <summary>
/// 存储过程执行函数
/// </summary>
/// <param name="strSpName">存储过程名</param>
/// <param name="ht">参数信息集</param>
/// <param name="strParameterArray">需返回的参数名数组</param>
/// <returns>存有返回值的HashTable</returns>
public void ExecStoredProcedure(string strSpName,Hashtable ht,string[] strParameterArray)
{
SqlCommand comm = new SqlCommand(strSpName,Conn);
comm.CommandType = CommandType.StoredProcedure;
IDictionaryEnumerator htEnumerator = ht.GetEnumerator();
while (htEnumerator.MoveNext())
comm.Parameters.Add(htEnumerator.Key.ToString(),htEnumerator.Value);
foreach (string strParameterName in strParameterArray)
comm.Parameters[strParameterName].Direction = ParameterDirection.Output;
try
{
OpenConn(); //打开数据库链接
comm.ExecuteNonQuery();
}
finally
{
CloseConn(); //关闭数据库链接
}
ht.Clear();
foreach (string strParameterName in strParameterArray)
ht.Add(strParameterName,comm.Parameters[strParameterName].Value);
}
/// <summary>
/// 存储过程执行函数
/// </summary>
/// <param name="strSpName">存储过程名</param>
/// <param name="ht">参数信息集</param>
public void ExecStoredProcedure(string strSpName,Hashtable ht)
{
SqlCommand comm = new SqlCommand(strSpName,Conn);
comm.CommandType = CommandType.StoredProcedure;
IDictionaryEnumerator htEnumerator = ht.GetEnumerator();
while (htEnumerator.MoveNext())
comm.Parameters.Add(htEnumerator.Key.ToString(),htEnumerator.Value);
try
{
OpenConn(); //打开数据库链接
comm.ExecuteNonQuery();
}
finally
{
CloseConn(); //关闭数据库链接
}
}
调用方法:
Hashtable ht = new Hashtable();
ht.Add("@variable_1","参数1");
ht.Add("@variable_2","参数2");
ht.Add("@variable_3","参数3");
//带返回数据
string[] variableList = {"@variable_2","@variable_3"};
ExecStoredProcedure("存储过程名",Hashtable ht,variableList);
//返回数据在Hashtable ht相对应的键中
//不带返回数据
ExecStoredProcedure("存储过程名",Hashtable ht);
//ExecStoredProcedure方法是一个2次重载的方法
posted on 2006-04-24 15:19 ............. 阅读(1721) 评论(0) 编辑 收藏 举报