>public class csStoredProcBase >{ > private ArrayList _colParameters = new ArrayList(); > public ArrayList colParameters > { > get { return _colParameters; } > } > > private string _sStoredProcName = ""; > public string sStoredProcName > { > get { return _sStoredProcName; } > set {_sStoredProcName = value; } > } > > public void AddParameter(string sParameterName, int iValue) > { > _AddParameter(sParameterName, iValue); > } > public void AddParameter(string sParameterName, string sValue) > { > _AddParameter(sParameterName, sValue); > } > private void _AddParameter(string sParameterName, object oValue) > { > SqlParameter pParam = new SqlParameter(); > pParam.ParameterName = sParameterName; > pParam.Value = oValue; > _colParameters.Add(pParam); > } > >} >>
>class csGetProjects : csStoredProcBase >{ > public csGetProjects() > { > this.sStoredProcName = "bm_GetProjects"; > } > > private int _iAppKey = -1; > public int iAppKey > { > get { return _iAppKey; } > set > { > _iAppKey = value; > this.AddParameter("@AppKey", _iAppKey); > } > } > > private int _iCategoryKey = -1; > public int iCategoryKey > { > get { return _iCategoryKey; } > set > { > _iCategoryKey = value; > this.AddParameter("@CategoryKey", _iCategoryKey); > } > } > > private int _iStatusKey = -1; > public int iStatusKey > { > get { return _iStatusKey; } > set > { > _iStatusKey = value; > this.AddParameter("@StatusKey", _iStatusKey); > } > } > >} > >>
>public static SqlDataReader ExecuteStoredProc(csStoredProcBase oStoredProc) >{ > SqlDataReader rdrRetVal = null; > _oException = null; > > rdrRetVal = _oDataAccess.ExecuteReader(oStoredProc.sStoredProcName, CommandType.StoredProcedure, oStoredProc.colParameters); > > if (_oDataAccess.oException != null) > { > _oException = _oDataAccess.oException; > rdrRetVal = null; > } > > return rdrRetVal; >} >>
>csGetProjects oGetProjects = new csGetProjects(); >oGetProjects.iAppKey = 7; >SqlDataReader oRdr = csAppDataAccess.ExecuteStoredProc(oGetProjects); > > >SqlDataReader oRdr2 = csAppDataAccess.ExecuteStoredProc(new csGetProjects()); >>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; using System.Drawing; namespace ConsoleApplication1 { class SpCallTest { static void Main(string[] args) { DataTable t1 = new DataTable(); DataTable t2 = new DataTable(); t1.Load( GetReaderFromSP("[CustOrderHist]", new List<SqlParameter> { new SqlParameter("@customerID","BONAP")}) ); t2.Load( GetReaderFromSP("[CustOrdersDetail]", new List<SqlParameter> { new SqlParameter("@OrderID",10563)}) ); ShowDataForm f1 = new ShowDataForm(t1, "Customer Order History"); f1.ShowDialog(); ShowDataForm f2 = new ShowDataForm(t2, "Customer Orders Detail"); f2.ShowDialog(); } private static SqlDataReader GetReaderFromSP(string spName, List<SqlParameter> parameters) { SqlCommand cmd = new SqlCommand(); cmd.Connection = new SqlConnection(@"server=.\sqlExpress;trusted_connection=yes;database=Northwind"); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = spName; foreach (var p in parameters) { cmd.Parameters.Add(p); } cmd.Connection.Open(); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return rdr; } } public class ShowDataForm : Form { public ShowDataForm(DataTable tbl, string caption) { this.dgv = new System.Windows.Forms.DataGridView(); this.dgv.Location = new System.Drawing.Point(0, 0); this.dgv.Dock = DockStyle.Fill; this.dgv.DataSource = tbl; this.Text = caption; this.Controls.Add(this.dgv); this.ClientSize = new System.Drawing.Size(1024, 768); } private System.Windows.Forms.DataGridView dgv; } }I hardcoded the connection there but it is a sample:)
NorthwindDataContext nw = new NorthwindDataContext(); var result = nw.CustOrderHist("BONAP");Thinking context might be a static app element it would even boil down to: