(C#) ASP.NET MySQL Database Class |
(C#) ASP.NET MySQL Database Class ตัวอย่างนี้ผมได้ออกแบบ Database Class ไว้สำหรับการจัดการกับฐานข้อมูล ASP กับ MySQL โดยใช้ NameSpace ของ MySql.Data.MySqlClient ซึ่งมีฟังก์ชั่นครบในการใช้งานครับ ไม่ว่าจะเป็นการ เพิ่ม/ลบ/แก้ไข/อ่าน สำหรับตัวอย่างนี้ผมเขียนบน Framework 2.0,3.5 ครับ
Instance NameSpace
C#Using System.Data;
Using MySql.Data.MySqlClient;
ASP.NET & MySql.Data.MySqlClient
Language Code : VB.NET || C#
Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"/>
</appSettings>
</configuration>
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 MySql.Data.MySqlClient;
public partial class clsDatabase : System.Web.UI.Page
{
private MySqlConnection objConn;
private MySqlCommand objCmd;
private MySqlTransaction Trans;
private String strConnString;
public clsDatabase()
{
strConnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
}
public MySqlDataReader QueryDataReader(String strSQL)
{
MySqlDataReader dtReader;
objConn = new MySqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new MySqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
return dtReader; //*** Return DataReader ***//
}
public DataSet QueryDataSet(String strSQL)
{
DataSet ds = new DataSet();
MySqlDataAdapter dtAdapter = new MySqlDataAdapter();
objConn = new MySqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new MySqlCommand();
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)
{
MySqlDataAdapter dtAdapter;
DataTable dt = new DataTable();
objConn = new MySqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
dtAdapter = new MySqlDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
return dt; //*** Return DataTable ***//
}
public Boolean QueryExecuteNonQuery(String strSQL)
{
objConn = new MySqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new MySqlCommand();
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 MySqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new MySqlCommand();
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 MySqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void TransExecute(String strSQL)
{
objCmd = new MySqlCommand();
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;
}
}
AspNetDatabase.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="MySql.Data.MySqlClient"%>
<%@ Page Language="C#" Debug="true"%>
<script runat="server">
clsDatabase clsDB = new clsDatabase();
void Page_Load(object sender, EventArgs e)
{
myDataReader();
myDataSet();
myDataTable();
myQueryExecuteScalar();
myExecuteNonQuery();
myExecuteTransaction();
}
//*** DataReader ***//
void myDataReader()
{
String strSQL;
MySqlDataReader dtReader;
strSQL = "SELECT * FROM customer ";
dtReader = clsDB.QueryDataReader(strSQL);
this.myDataGrid1.DataSource = dtReader;
this.myDataGrid1.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.myDataGrid2.DataSource = ds.Tables[0].DefaultView;
this.myDataGrid2.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.myDataGrid3.DataSource = dt;
this.myDataGrid3.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();
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Database Class</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid id="myDataGrid1" runat="server"></asp:DataGrid>
<br>
<br>
<asp:DataGrid id="myDataGrid2" runat="server"></asp:DataGrid><br>
<br>
<asp:DataGrid id="myDataGrid3" runat="server"></asp:DataGrid><br>
<table style="WIDTH: 300px" border="1">
<tr>
<td style="WIDTH: 93px">
<asp:Label id="lblHeaderCustomerID" runat="server" Text="CustomerID"></asp:Label></td>
<td style="WIDTH: 213px">
<asp:Label id="lblCustomerID" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 93px">
<asp:Label id="lblHeaderName" runat="server" Text="Name"></asp:Label></td>
<td style="WIDTH: 213px">
<asp:Label id="lblName" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 93px">
<asp:Label id="lblHeaderEmail" runat="server" Text="Email"></asp:Label></td>
<td style="WIDTH: 213px; HEIGHT: 23px">
<asp:Label id="lblEmail" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 93px">
<asp:Label id="lblHeaderCountryCode" runat="server" Text="CountryCode"></asp:Label></td>
<td style="WIDTH: 213px; HEIGHT: 23px">
<asp:Label id="lblCountryCode" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 93px">
<asp:Label id="lblHeaderBudget" runat="server" Text="Budget"></asp:Label></td>
<td style="WIDTH: 213px; HEIGHT: 21px">
<asp:Label id="lblBudget" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 93px">
<asp:Label id="lblHeaderUsed" runat="server" Text="Used"></asp:Label></td>
<td style="WIDTH: 213px; HEIGHT: 21px">
<asp:Label id="lblUsed" runat="server"></asp:Label></td>
</tr>
</table>
<br>
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
ASP.NET MySql.Data.MySqlClient - Parameter Query
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-12-05 17:39:50 /
2017-03-29 10:17:02 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|