欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 通用的連接數(shù)據(jù)庫(kù)實(shí)例代碼

 更新時(shí)間:2013年08月14日 11:14:14   作者:  
數(shù)據(jù)庫(kù)連接是所有程序開(kāi)發(fā)是會(huì)用到的,只是不同程序與數(shù)據(jù)庫(kù)連接的方法不一樣,下面我來(lái)介紹asp.net中數(shù)據(jù)庫(kù)連接代碼,有需要的朋友可以參考一下

View Code

復(fù)制代碼 代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<center>
<h2><font face="宋體">訪問(wèn)數(shù)據(jù)庫(kù)的通用代碼實(shí)例</font>
</h2>
</center>
<body>
    <form id="form1" runat="server">
    <div>

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

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

asp.net頁(yè)面

復(fù)制代碼 代碼如下:

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)
    {
        //通用數(shù)據(jù)庫(kù)連接代碼,這里以連接Access數(shù)據(jù)庫(kù)為測(cè)試示例
        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)
    {

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

本段程序的核心代碼為

復(fù)制代碼 代碼如下:

//選擇的數(shù)據(jù)庫(kù)類型為“SQLServer”,創(chuàng)建SqlConnection類數(shù)據(jù)庫(kù)連接對(duì)象
case "SQLServer":
           MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
                break;
case "Oracle":
           MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
                break;
            //選擇的數(shù)據(jù)庫(kù)類型為“Access”,創(chuàng)建OleDbConnection類數(shù)據(jù)庫(kù)連接對(duì)象
case "Access":
           MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
                break;
            //選擇的數(shù)據(jù)庫(kù)類型為“DB2”,創(chuàng)建OleDbConnection類數(shù)據(jù)庫(kù)連接對(duì)象
case "DB2":
           MyConnection = new System.Data.Odbc.OdbcConnection(MyConnectionString);
                break;
default:
            MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
                break;


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

相關(guān)文章

最新評(píng)論