您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關asp.net 中怎么連接數據庫,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<center>
<h3><font face="宋體">訪問數據庫的通用代碼實例</font>
</h3>
</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;
關于asp.net 中怎么連接數據庫就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。