• C# 执行存储过程 方法


    功能 :  根据调用的方法名称  反射动态调用  sql Command 的方法

    现定义一个类用来存储存储过程参数
      /// <summary>
        /// 存储过程的属性 
        /// ProcName 存储过程的名称
        /// MethodName 执行SqlCommand 方法的名称
        /// PrmList 存储过程的参数
        /// </summary>
        public class ExeProc
        {
            public string ProcName;
            public string MethodName;
            public object[] PrmValue;
        }

    根据制定的存储过程的名称

    和参数  来执行指定的存储过程 和 调用 sqlCommand 的方法

    执行的代码
     public class DataHelper
        {
            private string connString = null;
            public DataHelper(string conStr)
            {
                this.connString = conStr;
            }
            /// <summary>
            ///  执行存储过程
            /// </summary>
            /// <param name="ep">执行存储过程的属性 
            /// ProcName 存储过程的名称
            /// MethodName 执行SqlCommand 方法的名称
            /// PrmList 存储过程的参数
            /// </param>
            /// <returns>返回执行的结果</returns>
            public object ExecProcRetObj(ExeProc ep)
            {
                if (this.connString != null && this.connString != string.Empty)
                {
                    try
                    {
                        SqlConnection con = new SqlConnection(this.connString);
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = con;
                        cmd.CommandText = "Exec " + ep.ProcName + " ";
                        foreach (object obj in ep.PrmValue)
                        {
                            cmd.CommandText += obj + ",";
                        }
                        cmd.CommandText = cmd.CommandText.Remove(cmd.CommandText.Length - 1, 1);
                        Type ty = cmd.GetType();
                        con.Open();
    
                        //用反射根据输入的方法名 执行对应的方法 
    
                        object retObj = ty.InvokeMember(ep.MethodName, BindingFlags.InvokeMethod, null, cmd, null);
                        if (retObj.GetType().FullName == "System.Data.SqlClient.SqlDataReader")
                        {
                            //将返回的object 转换成DataTable 
                            DataTable retDt = new DataTable();
                            retDt.Load(retObj as SqlDataReader);
                            con.Close();
                            con.Dispose();
                            return retDt;
                        }
    
                        return retObj;
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("获取数据发生错误\n" + ex.Message);
                    }
    
                }
                return null;
            }
    
    
    
    
        }
  • 相关阅读:
    第01组-Alpha冲刺总结
    pypy windows安装scrapy
    Python:pip无法使用No module named '_sysconfigdata_m_linux_x86_64-linux-gnu'
    Jenkins+Ansible+Gitlab:通过curl自动推送文件&gitlab自动触发构建
    zabbix安装及问题小结
    虚拟机中的jenkins无法访问&Nginx配置
    centos7 安装gitlab及简单配置
    kerberos&LDAP实现免密码登录搭建
    kerberos&ssh 原理、免密登录搭建
    自启动脚本/etc/rc.local文件
  • 原文地址:https://www.cnblogs.com/hzy168/p/2992621.html
Copyright © 2020-2023  润新知