• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Stand By .NET

.NET & SQL Server
  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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  .............  阅读(1727)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3