protected bool FillData(DataSet ds, string StoredProcName, string TableName) { try { this.oCommand.CommandText = StoredProcName; this.oAdapter.TableMappings.Clear(); if (TableName.Length != 0) this.oAdapter.TableMappings.Add("Table", TableName); this.oAdapter.Fill(ds); } catch (Exception ex) { this.ErrorMessage += ex.Message; this.CloseConnection(); return false; } return true; }~~Bonnie
>protected IDbDataAdapter _GetDataAdapter(string sQuery) >{ > IDbDataAdapter oAdapter = null; > > try > { > switch (_Provider) > { > // Sql Data Provider > case ProviderType.SQL: > oAdapter = new SqlDataAdapter(sQuery, _ConnectionString); > SqlCommandBuilder oSqlBuilder = new SqlCommandBuilder((SqlDataAdapter)oAdapter); > break; > > // OleDb Data Provider > case ProviderType.OleDb: > oAdapter = new OleDbDataAdapter(sQuery, _ConnectionString); > OleDbCommandBuilder oOleDbBuilder = new OleDbCommandBuilder((OleDbDataAdapter)oAdapter); > break; > > // ODBC Data Provider > case ProviderType.ODBC: > oAdapter = new OdbcDataAdapter(sQuery, _ConnectionString); > OdbcCommandBuilder oOdbcBuilder = new OdbcCommandBuilder((OdbcDataAdapter)oAdapter); > break; > } > } > catch (SqlException e) > { > } > return oAdapter; >} >>
>>public class MyDataAccess >>{ >> #region Declarations >> >> private IDbConnection oConnection; >> private IDbCommand oCommand; >> private IDbDataAdapter oAdapter; >> private IDataParameterCollection oParms; >> public string ConnectionString = ""; >> protected string ErrorMessage = ""; >> >> #endregion >> >> #region Constructor/Destructor >> >> public MyDataAccess() >> { >> MyConnection o = new MyConnection(); >> this.oConnection = o.GetConnection(); >> this.ConnectionString = o.ConnectionString; >> >> this.oCommand = this.GetIDbCommand(); >> this.oAdapter = this.GetIDbDataAdapter(this.oCommand); >> this.oCommand.Connection = this.oConnection; >> this.oCommand.CommandType = CommandType.StoredProcedure; >> } >> >> #endregion >> >> #region Get/Use IDb Interface Objects >> >> protected IDbCommand GetIDbCommand() >> { >> return new SqlCommand(); >> } >> protected IDbDataAdapter GetIDbDataAdapter(IDbCommand command) >> { >> return new SqlDataAdapter((SqlCommand)command); >> } >> protected IDbDataParameter GetIDbDataParameter(string ParmName, object ParmValue) >> { >> return new SqlParameter(ParmName, ParmValue); >> } >> protected void DeriveParameters(IDbCommand command) >> { >> SqlCommandBuilder.DeriveParameters((SqlCommand)command); >> } >> #endregion >> >> // Then there are the various protected methods for adding and setting Parameters, filling a DataSet, etc. >> // It's these various methods that get used in the classes that are sub-classed from this "base" class. >> // Here's just a few of them: >> >> protected void ClearParameters() >> { >> this.oCommand.Parameters.Clear(); >> this.ErrorMessage = ""; >> } >> >> protected void AddParms(string ParmName, object ParmValue) >> { >> try >> { >> if (ParmName.StartsWith(this.AtSign) == false) >> ParmName = this.AtSign + ParmName; >> >> if (ParmValue != DBNull.Value) >> this.oCommand.Parameters.Add(this.GetIDbDataParameter(ParmName, ParmValue)); >> } >> catch (Exception ex) >> { >> this.ErrorMessage += ex.Message; >> } >> } >> protected void AddParms(string ParmName, object ParmValue, ParameterDirection direction) >> { >> this.AddParms(ParmName, ParmValue); >> this.SetParmDirection(ParmName, direction); >> } >> protected bool FillData(DataSet ds, string StoredProcName) >> { >> try >> { >> this.oCommand.CommandText = StoredProcName; >> this.oAdapter.Fill(ds); >> } >> catch (Exception ex) >> { >> this.ErrorMessage += ex.Message; >> this.CloseConnection(); >> return false; >> } >> return true; >> } >> >>} >>>>
>>public class PersonsAccess : MyDataAccess >>{ >> #region Public Get Methods >> >> public DataSet GetPersonsOnReport(ref string Message, string ReportNumber, string Supplement) >> { >> PersonDataSet ds = new PersonDataSet(); >> >> this.ClearParameters(); >> this.AddParms("ReportNumber", ReportNumber); >> this.AddParms("Supplement", Supplement); >> this.FillData(ds, "csp_Persons_Get"); >> >> return ds; >> } >> >> // more methods >> >> #endregion >>} >>>>