A class that is boring for a few days ago, which is written for different databases. It still can't be said that it is complete because there are still many features that have not been written. Similarly, in the following code, only two of SQL and OLE are not supported for Oracle, MySQL, etc. Of course, this type of extension function can be used for other databases to join the file in the file! ^ o ^ =============================================== =====
Here I use an example to do source code: Join in Web.config
: ====================== --------------------------- ------------------------------
-------------------------------------------------- --------
Below is the DatabaseConnect.cs class file: ======================= ------------------- --------------------------------------------
Using system;
Using system.data;
Using system.configuration;
Namespace Test
{
///
A summary description of /// DatabaseConnect. ///
Public Class DatabaseConnect
{
Public String DataBaseType;
Public String DatabaseName;
Protected IDBConnection DataConnection;
Public DataBaseConnect ()
{
//
// Todo: Join the program code of the constructive function here
//
}
Public idbconnection dbconnection ()
{
// Determine the database type and open the database specified in Web.config. Returns the idbconnection value.
Switch (DatabaseType)
{
Case "SQL":
DataConnection = new system.data.sqlclient.sqlconnection (configurationSettings.appsettings [databaseename]);
Break;
Case "OLE":
DataConnection = new system.data.oledb.oledbConnection (ConfigurationSettings.Appsettings [DatabaseName]);
}
Return DataConnection;
}
}
}
=================================================
Built another class file: DatabaseTool.cs ========================== ---------------------------------- -------------------------------------------------- --------
Using system; use system.data; namespace test {///
/// DatabaseTool's summary description. ///
Public Class DatabaseTool
{
Private idbconnection dbconnection;
Private idbcommand cmd;
Private iDataReader DataReader;
Private DatabaseConnect Dbconnect = New DatabaseConnect ();
Public DatabaseTool ()
{
//
// Todo: Join the program code of the constructive function here
//
}
Public void Open (String DatabaseType, String DatabaseName)
{
// member function, parameter DatabaseType is the database type, the parameter DatabaseName is the key name in Web.Config
Dbconnect.databaseType = DatabaseType.toupper ();
Dbconnect.databaseName = DatabaseName;
DBConnection = dbconnect.dbconnection ();
DbConnection.Open ();
CMD = dbconnection.createCommand ();
}
Public void close ()
{
// The link is turned off.
cmd.dispose ();
dbconnection.close ();
dbconnection.dispose ();
}
Public iDataReader getData (String Sqlstring)
{
//data query. Returns a DataReader value.
cmd.commandtext = SQLSTRING;
DataReader = cmd.executeReader ();
Return DataReader;
}
Public int insertdata (String Sqlstring)
{
// Data insertion. Returns the number of rows.
cmd.commandtext = SQLSTRING;
Return cmd.executenonquery ();
}
Public int deletedata (String Sqlstring)
{
// Data Delete. Returns the number of rows.
cmd.commandtext = SQLSTRING;
Return cmd.executenonquery ();
}
Public int updatedata (String Sqlstring)
{
//Data Update. Returns the number of rows.
cmd.commandtext = SQLSTRING;
Return cmd.executenonquery ();
}
}
}
=============================================================================================================================================================================================================
After the class file is completed, it can be used! ^ O ^ below is a simple usage:
DatabaseTool DBTOOL = New DatabaseTool (); // Instantiates DBTool.Open ("SQL", "SqlConnection"); // Open Database String Sqlstring = "Select * from Administrator"; // Defines SQL Statement DataGrid.DataSource = DBTOOL.GETDATA (SQLSTRING); // Data is scheduled to be in the DataGrid. DGList.Database (); dbtool.close (); // database close
=====================================
You can also expand a lot in this class. For example, only a DataReader object is returned after this call, there are many useless, such as DataTable, etc.