asp. net generic connection database instance code
- 2020-06-19 10:05:20
- OfStack
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<center>
<h2><font face=" Song typeface "> A generic code instance that accesses a database </font>
</h2>
</center>
<body>
<form id="form1" runat="server">
<div>
<font face=" Song typeface ">
<p align="center">1. Please enter the appropriate database connection string </p>
<p align="center">
<asp:TextBox id="ConnStrTextBox" runat="server" Width="600"></asp:TextBox>
</p>
<p align="center">2. Please enter the corresponding 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 type of database 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="Button1_Click" Text=" Universal database connection code testing " />
</p>
<p align="center">
<asp:Label id="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
</p>
</form>
</font>
</div>
asp net page
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)
{
// Generic database connection code, here to connect Access The database is the 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=' Small gu '";
lblMessage.Text = "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
// Define the database connection string
string MyConnectionString = this.ConnStrTextBox.Text;
// Defines the query operation SQL statements
string MySQL = this.SqlTextTextBox.Text;
// Define the type of database to connect to as Access
string MyType = this.DBDropDownList.SelectedValue;
System.Data.IDbConnection MyConnection = null;
// Based on the database type, create the corresponding Connection object
switch (MyType)
{
// The selected database type is" SQLServer ", create SqlConnection Class database connection object
case "SQLServer":
MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
break;
case "Oracle":
MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
break;
// The selected database type is" Access ", create OleDbConnection Class database connection object
case "Access":
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
// The selected database type is" DB2 ", create OleDbConnection Class 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 CreateCommand() Method to generate Command object
System.Data.IDbCommand MyCommand = MyConnection.CreateCommand();
// Execution-Defined SQL The query
MyCommand.CommandText = strquery;
try
{
// Open database connection
MyConnection.Open();
// Define the result information for the query
String MyInfo = " Test connection successful! The total number of records meeting the query requirements is: " + MyCommand.ExecuteScalar().ToString() + " Article! ";
// 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
// The selected database type is" SQLServer ", create SqlConnection Class database connection object
case "SQLServer":
MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
break;
case "Oracle":
MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
break;
// The selected database type is" Access ", create OleDbConnection Class database connection object
case "Access":
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
// The selected database type is" DB2 ", create OleDbConnection Class 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 another connection we can add one more connection code.