asp.net 通用的連接數(shù)據(jù)庫實(shí)例代碼
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<center>
<h2><font face="宋體">訪問數(shù)據(jù)庫的通用代碼實(shí)例</font>
</h2>
</center>
<body>
<form id="form1" runat="server">
<div>
<font face="宋體">
<p align="center">1.請輸入相應(yīng)數(shù)據(jù)庫連接字符串</p>
<p align="center">
<asp:TextBox id="ConnStrTextBox" runat="server" Width="600"></asp:TextBox>
</p>
<p align="center">2.請輸入相應(yīng)SQL查詢命令語句</p>
<p align="center">
<asp:TextBox id="SqlTextTextBox" runat="server" Width="600"></asp:TextBox>
</p>
<p align="center">3.請選擇所連接的數(shù)據(jù)庫類型</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ù)庫連接代碼測試" />
</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)
{
//通用數(shù)據(jù)庫連接代碼,這里以連接Access數(shù)據(jù)庫為測試示例
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ù)庫連接字符串
string MyConnectionString = this.ConnStrTextBox.Text;
//定義查詢操作的SQL語句
string MySQL = this.SqlTextTextBox.Text;
//定義所要連接的數(shù)據(jù)庫類型為Access
string MyType = this.DBDropDownList.SelectedValue;
System.Data.IDbConnection MyConnection = null;
// 根據(jù)數(shù)據(jù)庫類型,創(chuàng)建相應(yīng)的 Connection 對象
switch (MyType)
{
//選擇的數(shù)據(jù)庫類型為“SQLServer”,創(chuàng)建SqlConnection類數(shù)據(jù)庫連接對象
case "SQLServer":
MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
break;
case "Oracle":
MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
break;
//選擇的數(shù)據(jù)庫類型為“Access”,創(chuàng)建OleDbConnection類數(shù)據(jù)庫連接對象
case "Access":
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
//選擇的數(shù)據(jù)庫類型為“DB2”,創(chuàng)建OleDbConnection類數(shù)據(jù)庫連接對象
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();
//執(zhí)行定義的SQL查詢語句
MyCommand.CommandText = strquery;
try
{
//打開數(shù)據(jù)庫連接
MyConnection.Open();
//定義查詢的結(jié)果信息
String MyInfo = "測試連接成功!符合查詢要求的記錄共有:" + MyCommand.ExecuteScalar().ToString() + "條!";
//輸出查詢結(jié)果信息
lblMessage.Text = MyInfo;
}
catch (Exception ex)
{
//輸出錯誤異常
Response.Write(ex.ToString());
}
finally
{
//關(guān)閉數(shù)據(jù)庫連接
MyConnection.Close();
}
}
}
本段程序的核心代碼為
//選擇的數(shù)據(jù)庫類型為“SQLServer”,創(chuàng)建SqlConnection類數(shù)據(jù)庫連接對象
case "SQLServer":
MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
break;
case "Oracle":
MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
break;
//選擇的數(shù)據(jù)庫類型為“Access”,創(chuàng)建OleDbConnection類數(shù)據(jù)庫連接對象
case "Access":
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
//選擇的數(shù)據(jù)庫類型為“DB2”,創(chuàng)建OleDbConnection類數(shù)據(jù)庫連接對象
case "DB2":
MyConnection = new System.Data.Odbc.OdbcConnection(MyConnectionString);
break;
default:
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
如果你要其它連接我們還可以增加一些連接代碼哦。
- asp.net 數(shù)據(jù)庫的連接和datatable類
- asp.net連接數(shù)據(jù)庫 增加,修改,刪除,查詢代碼
- ASP.NET2.0 SQL Server數(shù)據(jù)庫連接詳解
- asp.net 數(shù)據(jù)庫連接類代碼(SQL)
- asp.net LINQ中數(shù)據(jù)庫連接字符串的問題
- asp.net 數(shù)據(jù)庫連接池淺析
- ASP.NET 6種常用數(shù)據(jù)庫的連接方法
- ASP.NET web.config中數(shù)據(jù)庫連接字符串connectionStrings節(jié)的配置方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- ASP.NET 連接ACCESS數(shù)據(jù)庫的簡單方法
- ASP.NET連接MySql數(shù)據(jù)庫的2個方法及示例
- 在ASP.NET 2.0中操作數(shù)據(jù)之七十:配置數(shù)據(jù)庫連接和命令等級設(shè)置
- 在ASP.NET 2.0中操作數(shù)據(jù)之七十一:保護(hù)連接字符串及其它設(shè)置信息
相關(guān)文章
asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
asp.net 上傳圖片并作處理(生成縮略圖 、在圖片上增加文字水印、在圖片上生成圖片水?。┑膶?shí)例代碼,經(jīng)過測試!2013-06-06
Asp.net XMLHTTP封裝類(GET,Post發(fā)送和接收數(shù)據(jù))
XMLHTTP封裝類可以向遠(yuǎn)程發(fā)送URL和參數(shù),接受返回信息(無亂碼)2008-11-11
Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法,實(shí)現(xiàn)思路分為前后臺代碼和效果展示,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10
linq to sql 中,如何解決多條件查詢問題,答案,用表達(dá)式樹! (下)
在上一篇中,我們做了基于linq to sql 的多條件組合查詢,但通過監(jiān)視數(shù)據(jù)庫發(fā)現(xiàn),這樣做的成本比較高,每次都要取出全部的數(shù)據(jù)到內(nèi)存進(jìn)行篩選.2011-08-08
asp.net 2.0中利用Ajax2.0實(shí)現(xiàn)JSON傳送大量頁面數(shù)據(jù)
本人遇到一個程序頁面,要有很大量的數(shù)據(jù)進(jìn)行交互操作。2010-03-03
ASP.NET如何使用web服務(wù)的會話狀態(tài)
這篇文章主要介紹了ASP.NET如何使用web服務(wù)的會話狀態(tài),使用一個GridView中的會話對象來展示最近的計(jì)算結(jié)果,感興趣的小伙伴們可以參考一下2015-11-11

