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;
// 根據數據庫類型,創(chuàng)建相應的 Connection 對象
switch (MyType)
{
//選擇的數據庫類型為“SQLServer”,創(chuàng)建SqlConnection類數據庫連接對象
case "SQLServer":
MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
break;
case "Oracle":
MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
break;
//選擇的數據庫類型為“Access”,創(chuàng)建OleDbConnection類數據庫連接對象
case "Access":
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
//選擇的數據庫類型為“DB2”,創(chuàng)建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();
//執(zhí)行定義的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”,創(chuàng)建SqlConnection類數據庫連接對象
case "SQLServer":
MyConnection = new System.Data.SqlClient.SqlConnection(MyConnectionString);
break;
case "Oracle":
MyConnection = new System.Data.OracleClient.OracleConnection(MyConnectionString);
break;
//選擇的數據庫類型為“Access”,創(chuàng)建OleDbConnection類數據庫連接對象
case "Access":
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
//選擇的數據庫類型為“DB2”,創(chuàng)建OleDbConnection類數據庫連接對象
case "DB2":
MyConnection = new System.Data.Odbc.OdbcConnection(MyConnectionString);
break;
default:
MyConnection = new System.Data.OleDb.OleDbConnection(MyConnectionString);
break;
如果你要其它連接我們還可以增加一些連接代碼哦。
- asp.net 數據庫的連接和datatable類
- asp.net連接數據庫 增加,修改,刪除,查詢代碼
- ASP.NET2.0 SQL Server數據庫連接詳解
- asp.net 數據庫連接類代碼(SQL)
- asp.net LINQ中數據庫連接字符串的問題
- asp.net 數據庫連接池淺析
- ASP.NET 6種常用數據庫的連接方法
- ASP.NET web.config中數據庫連接字符串connectionStrings節(jié)的配置方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- ASP.NET 連接ACCESS數據庫的簡單方法
- ASP.NET連接MySql數據庫的2個方法及示例
- 在ASP.NET 2.0中操作數據之七十:配置數據庫連接和命令等級設置
- 在ASP.NET 2.0中操作數據之七十一:保護連接字符串及其它設置信息
相關文章
Asp.net XMLHTTP封裝類(GET,Post發(fā)送和接收數據)
XMLHTTP封裝類可以向遠程發(fā)送URL和參數,接受返回信息(無亂碼)2008-11-11Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現方法
這篇文章主要介紹了Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現方法,實現思路分為前后臺代碼和效果展示,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10linq to sql 中,如何解決多條件查詢問題,答案,用表達式樹! (下)
在上一篇中,我們做了基于linq to sql 的多條件組合查詢,但通過監(jiān)視數據庫發(fā)現,這樣做的成本比較高,每次都要取出全部的數據到內存進行篩選.2011-08-08asp.net 2.0中利用Ajax2.0實現JSON傳送大量頁面數據
本人遇到一個程序頁面,要有很大量的數據進行交互操作。2010-03-03