asp.net common Connection Database Instance code _ practical Skills

Source: Internet
Author: User

View Code

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>


<center>
</center>
<body>
<form id= "Form1" runat= "Server" >
<div>

<font face= "Song Body" >
<p align= "center" >1. Please enter the corresponding database connection string </p>
<p align= "center" >
<asp:textbox id= "Connstrtextbox" runat= "Server" width= "></asp:TextBox>"
</p>
<p align= "center" >2. Please enter the appropriate SQL query command statement </p>
<p align= "center" >
<asp:textbox id= "Sqltexttextbox" runat= "Server" width= "></asp:TextBox>"
</p>
<p align= "Center" >3. Select the type of database you are connecting 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 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 Code code 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)
{
Common database Connection code, here to connect an Access database as a test sample
if (! IsPostBack)
{
Connstrtextbox.text = "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "+ Server.MapPath (" User.mdb ");
Sqltexttextbox.text = "Select COUNT (*) from Info Where name= ' Xiao gu '";
Lblmessage.text = "";
}
}
protected void Button1_Click (object sender, EventArgs e)
{

Defining a database connection string
String myconnectionstring = this. Connstrtextbox.text;
Define SQL statements for query operations
String MySQL = this. Sqltexttextbox.text;
Define the type of database you want to connect to is access
String MyType = this. Dbdropdownlist.selectedvalue;
System.Data.IDbConnection myconnection = null;
Create the appropriate Connection object based on the database type
Switch (MyType)
{
The selected database type is "SQL Server", creating the SqlConnection class database connection object
Case "SQL Server":
myconnection = new System.Data.SqlClient.SqlConnection (myconnectionstring);
Break
Case "Oracle":
myconnection = new System.Data.OracleClient.OracleConnection (myconnectionstring);
Break
The selected database type is "Access" and creates a OleDbConnection class database connection object
Case "Access":
myconnection = new System.Data.OleDb.OleDbConnection (myconnectionstring);
Break
Select a database type of "DB2" to create a 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)
{
To generate a Command object using the CreateCommand () method
System.Data.IDbCommand mycommand = Myconnection.createcommand ();
Execute a defined SQL query statement
myCommand.CommandText = strquery;
Try
{
Open a database connection
Myconnection.open ();
Define the result information for a query
String MyInfo = "Test connection succeeded!" The records that meet the query requirements are: "+ mycommand.executescalar (). ToString () + "Bar!" ";
Output Query Results information
Lblmessage.text = MyInfo;
}
catch (Exception ex)
{
Output Error exception
Response.Write (ex. ToString ());
}
Finally
{
To close a database connection
Myconnection.close ();
}
}
}

The core code for this procedure is

Copy Code code as follows:

The selected database type is "SQL Server", creating the SqlConnection class database connection object
Case "SQL Server":
myconnection = new System.Data.SqlClient.SqlConnection (myconnectionstring);
Break
Case "Oracle":
myconnection = new System.Data.OracleClient.OracleConnection (myconnectionstring);
Break
The selected database type is "Access" and creates a OleDbConnection class database connection object
Case "Access":
myconnection = new System.Data.OleDb.OleDbConnection (myconnectionstring);
Break
Select a database type of "DB2" to create a 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 also add some connection code OH.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.