(C#) ASP.NET Microsoft Access Database Class (Visual Studio 2005,2008,2010 - .NET 2.0,3.5,4.0) |
(C#) ASP.NET Microdot Access Database Class (Visual Studio 2005,2008,2010 - .NET 2.0,3.5,4.0) มาดูตัวอย่างของ Database Class ที่ผมเขียนบน Visual Studio 2005 (Framework 2.0) , Visual Studio 2008 (Framework 3.5) และ Visual Studio 2010 (Framework 4.0) ไว้สำหรับการจัดการกับฐานข้อมูล Microsoft Access โดยเฉพาะ ถ้าสนใจก็ดาวน์โหลดไปใช้งานหรือจะศึกษาพัฒนาเพิ่มก็ตามสบายครับ
Instance NameSpace
C#Using System.Data;
Using System.Data.OleDb;
ASP.NET & System.Data.OleDb
Language Code : VB.NET || C#
App_Code/clsDatabase.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class clsDatabase : System.Web.UI.Page
{
private OleDbConnection objConn;
private OleDbCommand objCmd;
private OleDbTransaction Trans;
private String strConnString;
public clsDatabase()
{
strConnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
}
public OleDbDataReader QueryDataReader(String strSQL)
{
OleDbDataReader dtReader;
objConn = new OleDbConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
return dtReader; //*** Return DataReader ***//
}
public DataSet QueryDataSet(String strSQL)
{
DataSet ds = new DataSet();
OleDbDataAdapter dtAdapter = new OleDbDataAdapter();
objConn = new OleDbConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
return ds; //*** Return DataSet ***//
}
public DataTable QueryDataTable(String strSQL)
{
OleDbDataAdapter dtAdapter;
DataTable dt = new DataTable();
objConn = new OleDbConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
dtAdapter = new OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
return dt; //*** Return DataTable ***//
}
public Boolean QueryExecuteNonQuery(String strSQL)
{
objConn = new OleDbConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
return true; //*** Return True ***//
}
catch (Exception)
{
return false; //*** Return False ***//
}
}
public Object QueryExecuteScalar(String strSQL)
{
Object obj;
objConn = new OleDbConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
obj = objCmd.ExecuteScalar(); //*** Return Scalar ***//
return obj;
}
catch (Exception)
{
return null; //*** Return Nothing ***//
}
}
public void TransStart()
{
objConn = new OleDbConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void TransExecute(String strSQL)
{
objCmd = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.Transaction = Trans;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
}
public void TransRollBack()
{
Trans.Rollback();
}
public void TransCommit()
{
Trans.Commit();
}
public void Close()
{
objConn.Close();
objConn = null;
}
}
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
clsDatabase clsDB = new clsDatabase();
protected void Page_Load(object sender, EventArgs e)
{
myDataReader();
myDataSet();
myDataTable();
myQueryExecuteScalar();
myExecuteNonQuery();
myExecuteTransaction();
}
//*** DataReader ***//
void myDataReader()
{
String strSQL;
OleDbDataReader dtReader;
strSQL = "SELECT * FROM customer ";
dtReader = clsDB.QueryDataReader(strSQL);
this.myGridView1.DataSource = dtReader;
this.myGridView1.DataBind();
//*** Bind Rows ***//
/*
if(dtReader.HasRows == true)
{
dtReader.Read();
this.lblCustomerID.Text = dtReader["CustomerID"].ToString();
this.lblName.Text = dtReader["Name"].ToString();
this.lblEmail.Text = dtReader["Email"].ToString();
this.lblCountryCode.Text = dtReader["CountryCode"].ToString();
this.lblBudget.Text = dtReader["Budget"].ToString();
this.lblUsed.Text = dtReader["Used"].ToString();
}
*/
clsDB.Close();
}
//*** DataSet ***//
void myDataSet()
{
String strSQL;
DataSet ds;
strSQL = "SELECT * FROM customer ";
ds = clsDB.QueryDataSet(strSQL);
this.myGridView2.DataSource = ds.Tables[0].DefaultView;
this.myGridView2.DataBind();
//*** Bind Rows ***//
if (ds.Tables[0].Rows.Count > 0)
{
this.lblCustomerID.Text = ds.Tables[0].Rows[0]["CustomerID"].ToString();
this.lblName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
this.lblEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();
this.lblCountryCode.Text = ds.Tables[0].Rows[0]["CountryCode"].ToString();
this.lblBudget.Text = ds.Tables[0].Rows[0]["Budget"].ToString();
this.lblUsed.Text = ds.Tables[0].Rows[0]["Used"].ToString();
}
clsDB.Close();
}
//*** DataTable ***//
void myDataTable()
{
String strSQL;
DataTable dt;
strSQL = "SELECT * FROM customer ";
dt = clsDB.QueryDataTable(strSQL);
this.myGridView3.DataSource = dt;
this.myGridView3.DataBind();
//*** Bind Rows ***//
if (dt.Rows.Count > 0)
{
this.lblCustomerID.Text = dt.Rows[0]["CustomerID"].ToString();
this.lblName.Text = dt.Rows[0]["Name"].ToString();
this.lblEmail.Text = dt.Rows[0]["Email"].ToString();
this.lblCountryCode.Text = dt.Rows[0]["CountryCode"].ToString();
this.lblBudget.Text = dt.Rows[0]["Budget"].ToString();
this.lblUsed.Text = dt.Rows[0]["Used"].ToString();
}
clsDB.Close();
}
//*** Execute Scalar ***//
void myQueryExecuteScalar()
{
String strSQL;
strSQL = "SELECT MAX(Budget) FROM customer ";
this.lblText.Text = clsDB.QueryExecuteScalar(strSQL).ToString();
clsDB.Close();
}
//*** ExecuteNonQuery ***//
void myExecuteNonQuery()
{
String strSQL1, strSQL2, strSQL3;
//*** Insert ***//
strSQL1 = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES('C005','Weerachai Nukitram','[email protected]','TH','200000','100000')";
if (clsDB.QueryExecuteNonQuery(strSQL1) == true)
{
//*** Condition Success ***//
}
else
{
//*** Condition Error ***//
}
clsDB.Close();
//*** Update ***//
strSQL2 = "UPDATE customer SET Budget = '3000000' WHERE CustomerID = 'C005' ";
if (clsDB.QueryExecuteNonQuery(strSQL2) == true)
{
//*** Condition Success ***//
}
else
{
//*** Condition Error ***//
}
clsDB.Close();
//*** Delete ***//
strSQL3 = "DELETE FROM customer WHERE CustomerID = 'C005' ";
if (clsDB.QueryExecuteNonQuery(strSQL3) == true)
{
//*** Condition Success ***//
}
else
{
//*** Condition Error ***//
}
clsDB.Close();
}
//*** Execute Transaction ***'
void myExecuteTransaction()
{
String strSQL1, strSQL2, strSQL3;
//*** Start Transaction ***//
clsDB.TransStart();
try
{
//*** Insert ***//
strSQL1 = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES('C005','Weerachai Nukitram','[email protected]','TH','200000','100000')";
clsDB.TransExecute(strSQL1); //*** Execute Query 1 ***//
//*** Update ***//
strSQL2 = "UPDATE customer SET Budget = '3000000' WHERE CustomerID = 'C005' ";
clsDB.TransExecute(strSQL2); //*** Execute Query 2 ***//
//*** Delete ***//
strSQL3 = "DELETE FROM customer WHERE CustomerID = 'C005' ";
clsDB.TransExecute(strSQL3); //*** Execute Query 3 **//
//*** Commit Transaction ***//
clsDB.TransCommit();
}
catch (Exception)
{
//*** RollBack Transaction ***//
clsDB.TransRollBack();
}
clsDB.Close();
}
}
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-16 20:23:01 /
2017-03-29 09:52:10 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|