程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 通用的連接數據庫實例代碼

asp.net 通用的連接數據庫實例代碼

編輯:ASP.NET基礎

View Code
復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<center>
<h2><font face="宋體">訪問數據庫的通用代碼實例</font>
</h2>
</center>
<body>
    <form id="form1" runat="server">
    <div>

    <font face="宋體">
<p align="center">1.請輸入相應數據庫連接字符串</p>
<p align="center">
<asp:TextBox id="ConnStrTextBox" runat="server" Width="600"></asp:TextBox>
</p>
<p align="center">2.請輸入相應SQL查詢命令語句</p>
<p align="center">
<asp:TextBox id="SqlTextTextBox" runat="server" Width="600"></asp:TextBox>
</p>
<p align="center">3.請選擇所連接的數據庫類型</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="通用數據庫連接代碼測試" />

</p>
<p align="center">
<asp:Label id="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
</p>
    </form>
</font>
</div>

asp.net頁面

復制代碼 代碼如下:
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)
    {
        //通用數據庫連接代碼,這裡以連接Access數據庫為測試示例
        if (!IsPostBack)
        {
           ConnStrTextBox.Text = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" + Server.MapPath("User.mdb");
           SqlTextTextBox.Text = "Select COUNT(*) From Info Where Name='小顧'";
            lblMessage.Text = "";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        //定義數據庫連接字符串
        string MyConnectionString = this.ConnStrTextBox.Text;
        //定義查詢操作的SQL語句
        string MySQL = this.SqlTextTextBox.Text;
        //定義所要連接的數據庫類型為Access
        string MyType = this.DBDropDownList.SelectedValue;
        System.Data.IDbConnection MyConnection = null;
        // 根據數據庫類型,創建相應的 Connection 對象
        switch (MyType)
        {
            //選擇的數據庫類型為“SQLServer”,創建SqlConnection類數據庫連接對象
            case "SQLServer":
                MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
                break;
            case "Oracle":
                MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
                break;
            //選擇的數據庫類型為“Access”,創建OleDbConnection類數據庫連接對象
            case "Access":
                MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
                break;
            //選擇的數據庫類型為“DB2”,創建OleDbConnection類數據庫連接對象
            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)
    {
        //使用 CreateCommand() 方法生成 Command 對象
        System.Data.IDbCommand MyCommand = MyConnection.CreateCommand();
        //執行定義的SQL查詢語句
        MyCommand.CommandText = strquery;
        try
        {
            //打開數據庫連接
            MyConnection.Open();
            //定義查詢的結果信息
            String MyInfo = "測試連接成功!符合查詢要求的記錄共有:" + MyCommand.ExecuteScalar().ToString() + "條!";
            //輸出查詢結果信息
            lblMessage.Text = MyInfo;
        }
        catch (Exception ex)
        {
            //輸出錯誤異常
            Response.Write(ex.ToString());
        }
        finally
        {
            //關閉數據庫連接
            MyConnection.Close();
        }
    }
}

本段程序的核心代碼為

復制代碼 代碼如下:
//選擇的數據庫類型為“SQLServer”,創建SqlConnection類數據庫連接對象
case "SQLServer":
           MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
                break;
case "Oracle":
           MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
                break;
            //選擇的數據庫類型為“Access”,創建OleDbConnection類數據庫連接對象
case "Access":
           MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
                break;
            //選擇的數據庫類型為“DB2”,創建OleDbConnection類數據庫連接對象
case "DB2":
           MyConnection = new System.Data.Odbc.OdbcConnection(MyConnectionString);
                break;
default:
            MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
                break;


如果你要其它連接我們還可以增加一些連接代碼哦。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved