using System;
using System.Data;
using System.Data.OleDb;
namespace DbClass
{
/// <summary>
/// Db_Class 的摘要說明。
/// </summary>
public class Db_Class
{
public OleDbConnection Conn;
//建構函式
public Db_Class()
{
Conn= new OleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");
}
//開啟資料來源連結
public OleDbConnection Db_Conn()
{
Conn.Open();
return Conn;
}
//返回DataReader資料集,下面的SQL可以動態產生
public OleDbDataReader Db_CreateReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回DataReader資料集,下面的SQL是預存程序
public OleDbDataReader Db_CommandReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
cmd.CommandType = CommandType.StoredProcedure;
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回資料DataSet資料集
public OleDbDataSet Db_CreateDataSet(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataAdapter Adpt= new OleDbDataAdapter(cmd,Conn);
DataSet Ds = new DataSet();
Adpt.Fill(Ds,"NewTable");
return Ds;
this.close();
}
//返回資料DataReader資料集,不需要返回資料的修改,刪除可以使用本函數
public bool Db_ExecuteNonquery(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
this.close();
}
//關閉資料連結
public void close()
{
Conn.Close();
}
}
}
使用方法如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;