C#의 스토리지 프로세스 방법

3008 단어
기능: 호출 방법 이름에 따라 sql Command를 동적으로 호출하는 방법
 
  
 ///
    ///
    /// ProcName
    /// MethodName SqlCommand
    /// PrmList
    ///

    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;
        }
        ///
        /// 
        ///

        ///
        /// ProcName
        /// MethodName SqlCommand
        /// PrmList
        ///
        ///
        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("
" + ex.Message);
                }

            }
            return null;
        }
    }

좋은 웹페이지 즐겨찾기