View Code
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<Center>
<H2> <font face = ""> common database access code instance </font>
</H2>
</Center>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Font face = "">
<P align = "center"> 1. Enter the database connection string </p>
<P align = "center">
<Asp: TextBox id = "ConnStrTextBox" runat = "server" Width = "600"> </asp: TextBox>
</P>
<P align = "center"> 2. Enter the SQL query command statement. </p>
<P align = "center">
<Asp: TextBox id = "SqlTextTextBox" runat = "server" Width = "600"> </asp: TextBox>
</P>
<P align = "center"> 3. Select the database type to connect to </p>
<P align = "center">
<Asp: DropDownList ID = "DBDropDownList" runat = "server" Width = "204px">
<Asp: ListItem Selected = "True"> Access </asp: ListItem>
<Asp: ListItem> SQLServer </asp: ListItem>
<Asp: ListItem> Oracle </asp: ListItem>
<Asp: ListItem> DB2 </asp: ListItem>
</Asp: DropDownList>
</P>
<P align = "center">
<Asp: Button ID = "Button1" runat = "server" onclick = "button#click" Text = "universal database connection code test"/>
</P>
<P align = "center">
<Asp: Label id = "lblMessage" runat = "server" Font-Bold = "True" ForeColor = "Red"> </asp: Label>
</P>
</Form>
</Font>
</Div>
Asp.net page
Copy codeThe Code is as follows:
Using System;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// General database connection code. The connection to the Access database is used as a test example.
If (! IsPostBack)
{
ConnStrTextBox. Text = "Provider = Microsoft. Jet. OLEDB.4.0; Data source =" + Server. MapPath ("User. mdb ");
SqlTextTextBox. Text = "Select COUNT (*) From Info Where Name = 'Gu '";
LblMessage. Text = "";
}
}
Protected void button#click (object sender, EventArgs e)
{
// Define the database connection string
String MyConnectionString = this. ConnStrTextBox. Text;
// Define the SQL statement for the query operation
String MySQL = this. SqlTextTextBox. Text;
// Define the database type to be connected as Access
String MyType = this. DBDropDownList. SelectedValue;
System. Data. IDbConnection MyConnection = null;
// Create a Connection object based on the database type
Switch (MyType)
{
// Select "SQLServer" as the database type and create a connection object for the SqlConnection database.
Case "SQLServer ":
MyConnection = new System. Data. SqlClient. SqlConnection (MyConnectionString );
Break;
Case "Oracle ":
MyConnection = new System. Data. OracleClient. OracleConnection (MyConnectionString );
Break;
// Set the database type to "Access" and create an OleDbConnection database connection object
Case "Access ":
MyConnection = new System. Data. OleDb. OleDbConnection (MyConnectionString );
Break;
// Select "DB2" as the database type and create an OleDbConnection database connection object
Case "DB2 ":
MyConnection = new System. Data. Odbc. OdbcConnection (MyConnectionString );
Break;
Default:
MyConnection = new System. Data. OleDb. OleDbConnection (MyConnectionString );
Break;
}
Execute (MyConnection, MySQL );
}
Public void Execute (System. Data. IDbConnection MyConnection, string strquery)
{
// Use the CreateCommand () method to generate the Command object
System. Data. IDbCommand MyCommand = MyConnection. CreateCommand ();
// Execute the defined SQL query statement
MyCommand. CommandText = strquery;
Try
{
// Open the database connection
MyConnection. Open ();
// Define query result information
String MyInfo = "test connection successful! There are a total of records that meet the query requirements: "+ MyCommand. ExecuteScalar (). ToString () +! ";
// Output query result information
LblMessage. Text = MyInfo;
}
Catch (Exception ex)
{
// Output error exception
Response. Write (ex. ToString ());
}
Finally
{
// Close the database connection
MyConnection. Close ();
}
}
}
The core code of this program is
Copy codeThe Code is as follows:
// Select "SQLServer" as the database type and create a connection object for the SqlConnection database.
Case "SQLServer ":
MyConnection = new System. Data. SqlClient. SqlConnection (MyConnectionString );
Break;
Case "Oracle ":
MyConnection = new System. Data. OracleClient. OracleConnection (MyConnectionString );
Break;
// Set the database type to "Access" and create an OleDbConnection database connection object
Case "Access ":
MyConnection = new System. Data. OleDb. OleDbConnection (MyConnectionString );
Break;
// Select "DB2" as the database type and create an OleDbConnection database connection object
Case "DB2 ":
MyConnection = new System. Data. Odbc. OdbcConnection (MyConnectionString );
Break;
Default:
MyConnection = new System. Data. OleDb. OleDbConnection (MyConnectionString );
Break;
If you want other connections, we can add some connection code.