zoukankan      html  css  js  c++  java
  • C# 执行存储过程

          SqlParameter[] paras = BuildParas(id, time, name);

          bool bRet = ExcuteProcedure(“数据库连接字符串”, "存储过程名", paras, "表名");

          public static SqlParameter[] BuildParas(string Id, DateTime Time, string Name)
          {
              SqlParameter[] paras = new SqlParameter[3];
              SqlParameter para1 = new SqlParameter();
              para1.Direction = ParameterDirection.Input;
              para1.ParameterName = "@pid";
              para1.SqlDbType = SqlDbType.VarChar;
              para1.Size = 32;
              para1.Value = Id;
              paras[0] = para1;

              SqlParameter para2 = new SqlParameter();
              para2.Direction = ParameterDirection.Input;
              para2.ParameterName = "@ptime";
              para2.SqlDbType = SqlDbType.DateTime;
              para2.Value = DateTime.Parse(Time);
              paras[1] = para2;

              SqlParameter para3 = new SqlParameter();
              para3.Direction = ParameterDirection.Input;
              para3.ParameterName = "@pname";
              para3.SqlDbType = SqlDbType.VarChar;
              para3.Size = 20;
              para3.Value = Name;
              paras[2] = para3;

              return paras;

          }

          public static bool ExcuteProcedure(string strDataBaseInfo, string procedureName, SqlParameter[] paras, string strTableName)
          {
              bool bRet = false;
              SqlCommand cmd = null;
              SqlConnection con = new SqlConnection(strDataBaseInfo);
              try
              {
                  con.Open();
                  cmd = new SqlCommand("select count(*) from " + strTableName + " where id='" + paras[0].Value.ToString() + "'", con);//目的是保证id唯一
                  int nExistCount = int.Parse(cmd.ExecuteScalar().ToString());//查询结果的第一行的第一列
                  if (nExistCount > 0)
                  {
                      return true;
                  }
                  cmd = null;
                  cmd = new SqlCommand(procedureName, con);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.Clear();
                  if (paras != null && paras.Length > 0)
                  {
                      for (int i = 0; i < paras.Length; i++)
                      {
                          cmd.Parameters.Add(paras[i]);
                      }
                  }
                  int nCount = cmd.ExecuteNonQuery();
                  if (nCount != -1)
                  {
                      bRet = true;
                  }
              }
              catch (Exception ex)
              {
                  

              }
              finally
              {
                  if (cmd != null)
                      cmd.Parameters.Clear();
                  if (con.State == ConnectionState.Open)
                      con.Close();
              }
              return bRet;

          }

  • 相关阅读:
    ipandao markdown mathjax版本
    我们是不是太关注于语法而忽略了算法
    SVGG.JS 入门教程
    关于ipandao编辑器手机访问换行问题
    启明星采购系统新版发布
    仿MSDN的帮助系统
    十分钟打造一款在线的数学公式编辑器
    Search Filter Syntax
    从华为养猪说起,聊聊我对中国计算机发展的一个遗憾-为何我们没有开发出自己的编程语言
    从.NET看微软的焦虑
  • 原文地址:https://www.cnblogs.com/xiaogongzhu/p/3828541.html
Copyright © 2011-2022 走看看