asp.net 通用的串連資料庫執行個體代碼_實用技巧

來源:互聯網
上載者:User

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;


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.