This two days wrote a class. Written for SQL Server and OLE databases. The source code is as follows: /classes/dbcontrol.cs ========================================= ============================================================================================================
Namespace Guestbook {///
public int RecordTotal {get {switch (DBTYPE) {case "SQL": sqlCmd.Cancel (); sqlCmd.CommandText = SQL_SELECT; SqlDataReader sqlDr; sqlDr = sqlCmd.ExecuteReader (); while (sqlDr.Read ()) {record_total ; } Sqldr.close (); break; case "ole": Olecmd.cancel (); OLECMD.CAMMANDTEXT = SQL_SELECT; OLEDBDATAREADER OLEDR; OLEDR = OLECMD.EXECUTEREADER (); while (OLEDR.READ ()) {record_total ;} OLEDR .Close (); break;} RECORD_TOTAL;}}
Public DBControl (String DBTYPE, STRING DBNAME) {// Heavy Duty Construction Function. DBTYPE = dbtype.toupper (); switch (dbtype.toupper ()) {case "sql": sqlconn = this.sqlconnect (dbname); oleconn.close (); oleconn.dispose (); break; case "ole": OLECONN = this.OleConnect (dbname); Oleconn.close (); Oleconn.dispose (); Break;}} public dbcontrol (): base () {// // Todo: Add to construct a program / /}
Public Void Open (String Dbtype, String DBName) {// Database file open. DBTYPE = dbtype.toupper (); switch (dbtype.toupper ()) {case "sql": Sqlconn = this.sqlconnect (dbname); Break; Case "OLE": Oleconn = this.OleConnect (dbname); Break;} }
Public SqlDataReader SqlgetReader (String StrQuery) {// Returns a SqlDataReader. For Sql server SQL_SELECT = strQuery; sqlCmd = new SqlCommand (strQuery, sqlConn); SqlDataReader dr; try {sqlCmd.Connection.Open ();} catch (Exception e) {throw e;} dr = sqlCmd.ExecuteReader (); Return DR;
Public OLEDBDataReader OlegetReader (String strquery) {// Returns an OLEDBDATAREADER. For OleDb SQL_SELECT = strQuery; oleCmd = new OleDbCommand (strQuery, oleConn); OleDbDataReader dr; try {oleCmd.Connection.Open ();} catch (Exception e) {throw e;} dr = oleCmd.ExecuteReader (); return DR;}
Public int SQLRunCommand (String strquery) {/ / executes a SQL statement. Includes record insertion, update, delete. Used for SQL Server SQL_SELECT = STRQUERY; SQLCMD = New SqlCommand (STRQUERY, SQLCONN); try {sqlcmd.connection.open ();} catch (Exception E) {throw E;} returnif ();}
Public int OLERUNCOMMAND (STRING STRQUERY) {/ Execute a SQL statement. Includes record insertion, update, delete. For OleDb SQL_SELECT = strQuery; oleCmd = new OleDbCommand (strQuery, oleConn); try {oleCmd.Connection.Open ();} catch (Exception e) {throw e;} return oleCmd.ExecuteNonQuery ();} public DataView SqlGetDataSet ( String strquery) {// Returns a DataSet. For Sql server SQL_SELECT = strQuery; sqlCmd = new SqlCommand (strQuery, sqlConn); try {sqlCmd.Connection.Open ();} catch (Exception e) {throw e;} SqlDataAdapter da = new SqlDataAdapter (); da.SelectCommand = Sqlcmd; da.fill (DS, "defaultTable"); return ds.tables ["defaultTable"]. defaultview;}
Public DataView OlegetDataSet {// Returns a DataSet. For OleDb SQL_SELECT = strQuery; oleCmd = new OleDbCommand (strQuery, oleConn); try {oleCmd.Connection.Open ();} catch (Exception e) {throw e;} OleDbDataAdapter da = new OleDbDataAdapter (); da.SelectCommand = OLECMD; DA.FILL (DS, "DefaultTable"); Return Ds.Tables ["defaultTable"]. defaultview;}
Public void close () {// database is closed. Switch (DBTYPE) {CASE "SQL": sqlcmd.cancel (); sqlcmd.dispose (); sqlconn.close (); sqlconn.dispose (); breaf; case "ole": Olecmd.cancel (); OlecMd.Dispose (); Oleconn.close (); oleconn.dispose (); break;} DS.CLEAR (); ds.dispose ();}}} ================= ==================================================== / Classes / DatabaseType / DBopen. CS ===================================================================================================================================================================================== =========== Using system; use system.data.sqlclient; use system.data.oledb; use system.configuration; namespace guestbook.classes.DatabaseType {///
Protected SqlConnection SqlConnect (SQLCONNAME) {SqlConnection CONN = New SqlConnection (ConfigurationSettings.AppSettings [DBNAME]); Return Conn;}
Protected OLEDBConnection OLECONNECT (String DBNAME) {OLEDBConnection Conn = New OLEDBConnection (ConfigurationSettings.AppSettings [DBNAME]); return conn;}}} ====================== ======================================================================================================================================================================================================================================= ============================================================================================================================================================================================================= ======= DBControl objdbcontrol = new dbcontrol (); string sql = "select * from content";