(C#) ASP.NET Oracle Database Class |
(C#) ASP.NET Oracle Database Class ตัวอย่างนี้ผมได้ออกแบบ Database Class ไว้สำหรับการจัดการกับฐานข้อมูล ASP กับ Oracle โดยใช้ NameSpace ของ System.Data.OracleClient ซึ่งมีฟังก์ชั่นครบในการใช้งานครับ ไม่ว่าจะเป็นการ เพิ่ม/ลบ/แก้ไข/อ่าน สำหรับตัวอย่างนี้ผมเขียนบน Framework 2.0,3.5 ครับ
Instance NameSpace
C#Using System.Data;
Using System.Data.OracleClient;
ASP.NET & System.Data.OracleClient
Oracle Create TNS Name (Net Service)
Language Code : VB.NET || C#
Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=TCDB;User Id=myuser;Password=mypassword;"/>
</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 System.Data.OracleClient;
public partial class clsDatabase : System.Web.UI.Page
{
private OracleConnection objConn;
private OracleCommand objCmd;
private OracleTransaction Trans;
private String strConnString;
public clsDatabase()
{
strConnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
}
public OracleDataReader QueryDataReader(String strSQL)
{
OracleDataReader dtReader;
objConn = new OracleConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new OracleCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
return dtReader; //*** Return DataReader ***//
}
public DataSet QueryDataSet(String strSQL)
{
DataSet ds = new DataSet();
OracleDataAdapter dtAdapter = new OracleDataAdapter();
objConn = new OracleConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new OracleCommand();
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)
{
OracleDataAdapter dtAdapter;
DataTable dt = new DataTable();
objConn = new OracleConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
dtAdapter = new OracleDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
return dt; //*** Return DataTable ***//
}
public Boolean QueryExecuteNonQuery(String strSQL)
{
objConn = new OracleConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new OracleCommand();
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 OracleConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new OracleCommand();
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 OracleConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void TransExecute(String strSQL)
{
objCmd = new OracleCommand();
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="System.Data.OracleClient"%>
<%@ 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;
OracleDataReader 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>
Oracle Create TNS Name (Net Service)
ASP.NET System.Data.OracleClient - Parameter Query
Note!! โปรดทราบ เนื่องจาก System.Data.OracleClient ได้ถูก Obsolete ไปแล้ว ฉะนั้นจึงจะต้องใช้ Oracle.DataAccess แทน โดยสามารถประกาศเรียกได้จาก
จากนั้นให้ใช้ using หรือ Import คอนเน็คเตอร์ใหม่คือ Oracle.DataAccess ส่วนคำสั่งอื่น ๆ ยังคงเหมือนเดิม
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-12-06 14:16:40 /
2017-03-29 10:53:14 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|