ASP.NET Class ใน ASP.NET Class คือแบบร่างที่ถูกออกแบบไว้สำหรับการ ทำงานใดทำงานหนึ่ง ในกลุ่มของคำสั่งที่เกี่ยวข้องกัน ซึ่งใน Class เองจะประกอบด้วย Method/Function หลาย ๆ Method และมี Property ที่ไว้รับตัวแปรจากภายนอก
Syntax
Public Class
Public Function myFunction1
End Function
Public Function myFunction2
End Function
.
.
.
End Class
ตัวอย่าง Class ที่ใช้สำหรับจัดการกับ Database
Database Class
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Public Class clsDatabase
Private objConn As OleDbConnection
Private objCmd As OleDbCommand
Private Trans As OleDbTransaction
Private strConnString As String
Public Sub New()
strConnString = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
End Sub
Public Function QueryDataReader(ByVal strSQL As String) As OleDbDataReader
Dim dtReader As OleDbDataReader
objConn = New OleDbConnection
With objConn
.ConnectionString = strConnString
.Open()
End With
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
Return dtReader '*** Return DataReader ***'
End Function
Public Function QueryDataSet(ByVal strSQL As String) As DataSet
Dim ds As New DataSet
Dim dtAdapter As New OleDbDataAdapter
objConn = New OleDbConnection
With objConn
.ConnectionString = strConnString
.Open()
End With
objCmd = New OleDbCommand
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
Return ds '*** Return DataSet ***'
End Function
Public Function QueryDataTable(ByVal strSQL As String) As DataTable
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
objConn = New OleDbConnection
With objConn
.ConnectionString = strConnString
.Open()
End With
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
Return dt '*** Return DataTable ***'
End Function
Public Function QueryExecuteNonQuery(ByVal strSQL As String) As Boolean
objConn = New OleDbConnection
With objConn
.ConnectionString = strConnString
.Open()
End With
Try
objCmd = New OleDbCommand
With objCmd
.Connection = objConn
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()
Return True '*** Return True ***'
Catch ex As Exception
Return False '*** Return False ***'
End Try
End Function
Public Function QueryExecuteScalar(ByVal strSQL As String) As Object
Dim obj As Object
objConn = New OleDbConnection
With objConn
.ConnectionString = strConnString
.Open()
End With
Try
objCmd = New OleDbCommand
With objCmd
.Connection = objConn
.CommandType = CommandType.Text
.CommandText = strSQL
End With
obj = objCmd.ExecuteScalar() '*** Return Scalar ***'
Return obj
Catch ex As Exception
Return Nothing '*** Return Nothing ***'
End Try
End Function
Public Function TransStart()
objConn = New OleDbConnection
With objConn
.ConnectionString = strConnString
.Open()
End With
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted)
End Function
Public Function TransExecute(ByVal strSQL As String) As Boolean
objCmd = New OleDbCommand
With objCmd
.Connection = objConn
.Transaction = Trans
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()
End Function
Public Function TransRollBack()
Trans.Rollback()
End Function
Public Function TransCommit()
Trans.Commit()
End Function
Public Sub Close()
objConn.Close()
objConn = Nothing
End Sub
End Class