Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > ASP.NET > ASP.NET Microsoft SQL Server (System.Data.SqlClient) > ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)


ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)

ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1) สำหรับ Database Class ตัวนี้เป็นของ Framework 1.1 ซึ่งผมได้พัฒนาบน Visual Studio .Net 2003 เหมาะสมกับผู้ที่ต้องการสร้าง Database Class ไว้สำหรับจัดการฐานข้อมูล SQL Server ซึ่งผมได้ทำการออกแบบไว้มีฟังก์ชั่นที่ครบครับ สามารถทำการ เพิ่ม/ลบ/แก้ไข/อ่าน ข้อมูลจากฐานข้อมูลได้อย่างง่ายดายครับ

Instance NameSpace

VB.NET
1.Imports System.Data
2.Imports System.Data.SqlClient


ASP.NET & System.Data.SqlClient

Language Code : VB.NET || C#

clsDatabase.vb

001.Imports System.Data
002.Imports System.Data.SqlClient
003.Imports System.Configuration
004.Public Class clsDatabase
005.    Private objConn As SqlConnection
006.    Private objCmd As SqlCommand
007.    Private Trans As SqlTransaction
008.    Private strConnString As String
009. 
010.    Public Sub New()
011.        strConnString = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
012.    End Sub
013. 
014.    Public Function QueryDataReader(ByVal strSQL As String) As SqlDataReader
015.        Dim dtReader As SqlDataReader
016.        objConn = New SqlConnection
017.        With objConn
018.            .ConnectionString = strConnString
019.            .Open()
020.        End With
021.        objCmd = New SqlCommand(strSQL, objConn)
022.        dtReader = objCmd.ExecuteReader()
023.        Return dtReader '*** Return DataReader ***'
024.    End Function
025. 
026.    Public Function QueryDataSet(ByVal strSQL As String) As DataSet
027.        Dim ds As New DataSet
028.        Dim dtAdapter As New SqlDataAdapter
029.        objConn = New SqlConnection
030.        With objConn
031.            .ConnectionString = strConnString
032.            .Open()
033.        End With
034.        objCmd = New SqlCommand
035.        With objCmd
036.            .Connection = objConn
037.            .CommandText = strSQL
038.            .CommandType = CommandType.Text
039.        End With
040.        dtAdapter.SelectCommand = objCmd
041.        dtAdapter.Fill(ds)
042.        Return ds   '*** Return DataSet ***'
043.    End Function
044. 
045.    Public Function QueryDataTable(ByVal strSQL As String) As DataTable
046.        Dim dtAdapter As SqlDataAdapter
047.        Dim dt As New DataTable
048.        objConn = New SqlConnection
049.        With objConn
050.            .ConnectionString = strConnString
051.            .Open()
052.        End With
053.        dtAdapter = New SqlDataAdapter(strSQL, objConn)
054.        dtAdapter.Fill(dt)
055.        Return dt '*** Return DataTable ***'
056.    End Function
057. 
058.    Public Function QueryExecuteNonQuery(ByVal strSQL As String) As Boolean
059.        objConn = New SqlConnection
060.        With objConn
061.            .ConnectionString = strConnString
062.            .Open()
063.        End With
064.        Try
065.            objCmd = New SqlCommand
066.            With objCmd
067.                .Connection = objConn
068.                .CommandType = CommandType.Text
069.                .CommandText = strSQL
070.            End With
071.            objCmd.ExecuteNonQuery()
072.            Return True '*** Return True ***'
073.        Catch ex As Exception
074.            Return False '*** Return False ***'
075.        End Try
076.    End Function
077. 
078.    Public Function QueryExecuteScalar(ByVal strSQL As String) As Object
079.        Dim obj As Object
080.        objConn = New SqlConnection
081.        With objConn
082.            .ConnectionString = strConnString
083.            .Open()
084.        End With
085.        Try
086.            objCmd = New SqlCommand
087.            With objCmd
088.                .Connection = objConn
089.                .CommandType = CommandType.Text
090.                .CommandText = strSQL
091.            End With
092.            obj = objCmd.ExecuteScalar()  '*** Return Scalar ***'
093.            Return obj
094.        Catch ex As Exception
095.            Return Nothing '*** Return Nothing ***'
096.        End Try
097.    End Function
098. 
099.    Public Function TransStart()
100.        objConn = New SqlConnection
101.        With objConn
102.            .ConnectionString = strConnString
103.            .Open()
104.        End With
105.        Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted)
106.    End Function
107. 
108.    Public Function TransExecute(ByVal strSQL As String) As Boolean
109.        objCmd = New SqlCommand
110.        With objCmd
111.            .Connection = objConn
112.            .Transaction = Trans
113.            .CommandType = CommandType.Text
114.            .CommandText = strSQL
115.        End With
116.        objCmd.ExecuteNonQuery()
117.    End Function
118. 
119.    Public Function TransRollBack()
120.        Trans.Rollback()
121.    End Function
122. 
123.    Public Function TransCommit()
124.        Trans.Commit()
125.    End Function
126. 
127.    Public Sub Close()
128.        objConn.Close()
129.        objConn = Nothing
130.    End Sub
131. 
132.End Class




Default.aspx.vb

001.Imports System.Data
002.Imports System.Data.SqlClient
003.Public Class _Default
004.    Inherits System.Web.UI.Page
005. 
006.#Region " Web Form Designer Generated Code "
007. 
008.    'This call is required by the Web Form Designer.
009.    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
010. 
011.    End Sub
012.    Protected WithEvents lblHeaderCustomerID As System.Web.UI.WebControls.Label
013.    Protected WithEvents lblCustomerID As System.Web.UI.WebControls.Label
014.    Protected WithEvents lblHeaderName As System.Web.UI.WebControls.Label
015.    Protected WithEvents lblName As System.Web.UI.WebControls.Label
016.    Protected WithEvents lblHeaderEmail As System.Web.UI.WebControls.Label
017.    Protected WithEvents lblEmail As System.Web.UI.WebControls.Label
018.    Protected WithEvents lblHeaderCountryCode As System.Web.UI.WebControls.Label
019.    Protected WithEvents lblCountryCode As System.Web.UI.WebControls.Label
020.    Protected WithEvents lblHeaderBudget As System.Web.UI.WebControls.Label
021.    Protected WithEvents lblBudget As System.Web.UI.WebControls.Label
022.    Protected WithEvents lblHeaderUsed As System.Web.UI.WebControls.Label
023.    Protected WithEvents lblUsed As System.Web.UI.WebControls.Label
024.    Protected WithEvents lblText As System.Web.UI.WebControls.Label
025.    Protected WithEvents myData1 As System.Web.UI.WebControls.DataGrid
026.    Protected WithEvents myDataGrid2 As System.Web.UI.WebControls.DataGrid
027.    Protected WithEvents myDataGrid1 As System.Web.UI.WebControls.DataGrid
028.    Protected WithEvents myDataGrid3 As System.Web.UI.WebControls.DataGrid
029. 
030.    'NOTE: The following placeholder declaration is required by the Web Form Designer.
031.    'Do not delete or move it.
032.    Private designerPlaceholderDeclaration As System.Object
033. 
034.    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
035.        'CODEGEN: This method call is required by the Web Form Designer
036.        'Do not modify it using the code editor.
037.        InitializeComponent()
038.    End Sub
039. 
040.#End Region
041. 
042.    Dim clsDB As New clsDatabase
043.    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
044.        'Put user code to initialize the page here
045.        Call myDataReader()
046.        Call myDataSet()
047.        Call myDataTable()
048.        Call myQueryExecuteScalar()
049.        Call myExecuteNonQuery()
050.        Call myExecuteTransaction()
051.    End Sub
052. 
053.    '*** DataReader ***'
054.    Sub myDataReader()
055.        Dim strSQL As String
056.        Dim dtReader As SqlDataReader
057.        strSQL = "SELECT * FROM customer "
058.        dtReader = clsDB.QueryDataReader(strSQL)
059.        Me.myDataGrid1.DataSource = dtReader
060.        Me.myDataGrid1.DataBind()
061. 
062.        '*** Bind Rows ***'
063.        'If dtReader.HasRows = True Then
064.        '    Me.lblCustomerID.Text = dtReader.Item("CustomerID")
065.        '    Me.lblName.Text = dtReader.Item("Name")
066.        '    Me.lblEmail.Text = dtReader.Item("Email")
067.        '    Me.lblCountryCode.Text = dtReader.Item("CountryCode")
068.        '    Me.lblBudget.Text = dtReader.Item("Budget")
069.        '    Me.lblUsed.Text = dtReader.Item("Used")
070.        'End If
071.        clsDB.Close()
072.    End Sub
073. 
074.    '*** DataSet ***'
075.    Sub myDataSet()
076.        Dim strSQL As String
077.        Dim ds As DataSet
078.        strSQL = "SELECT * FROM customer "
079.        ds = clsDB.QueryDataSet(strSQL)
080.        Me.myDataGrid2.DataSource = ds.Tables(0).DefaultView
081.        Me.myDataGrid2.DataBind()
082. 
083.        '*** Bind Rows ***'
084.        'If ds.Tables(0).Rows.Count > 0 Then
085.        '    Me.lblCustomerID.Text = ds.Tables(0).Rows(0)("CustomerID")
086.        '    Me.lblName.Text = ds.Tables(0).Rows(0)("Name")
087.        '    Me.lblEmail.Text = ds.Tables(0).Rows(0)("Email")
088.        '    Me.lblCountryCode.Text = ds.Tables(0).Rows(0)("CountryCode")
089.        '    Me.lblBudget.Text = ds.Tables(0).Rows(0)("Budget")
090.        '    Me.lblUsed.Text = ds.Tables(0).Rows(0)("Used")
091.        'End If
092.        clsDB.Close()
093.    End Sub
094. 
095.    '*** DataTable ***'
096.    Sub myDataTable()
097.        Dim strSQL As String
098.        Dim dt As DataTable
099.        strSQL = "SELECT * FROM customer "
100.        dt = clsDB.QueryDataTable(strSQL)
101.        Me.myDataGrid3.DataSource = dt
102.        Me.myDataGrid3.DataBind()
103. 
104.        '*** Bind Rows ***'
105.        If dt.Rows.Count > 0 Then
106.            Me.lblCustomerID.Text = dt.Rows(0)("CustomerID")
107.            Me.lblName.Text = dt.Rows(0)("Name")
108.            Me.lblEmail.Text = dt.Rows(0)("Email")
109.            Me.lblCountryCode.Text = dt.Rows(0)("CountryCode")
110.            Me.lblBudget.Text = dt.Rows(0)("Budget")
111.            Me.lblUsed.Text = dt.Rows(0)("Used")
112.        End If
113.        clsDB.Close()
114.    End Sub
115. 
116.    '*** Execute Scalar ***'
117.    Sub myQueryExecuteScalar()
118.        Dim strSQL As String
119.        strSQL = "SELECT MAX(Budget) FROM customer "
120.        Me.lblText.Text = clsDB.QueryExecuteScalar(strSQL)
121.        clsDB.Close()
122.    End Sub
123. 
124.    '*** ExecuteNonQuery ***'
125.    Sub myExecuteNonQuery()
126.        Dim strSQL1, strSQL2, strSQL3 As String
127. 
128.        '*** Insert ***'
129.        strSQL1 = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
130.        " VALUES('C005','Weerachai Nukitram','webmaster@thaicreate.com','TH','200000','100000')"
131.        If clsDB.QueryExecuteNonQuery(strSQL1) = True Then
132.            '*** Condition Success ***'
133.        Else
134.            '*** Condition Error ***'
135.        End If
136.        clsDB.Close()
137. 
138.        '*** Update ***'
139.        strSQL2 = "UPDATE customer SET Budget = '3000000' WHERE CustomerID = 'C005' "
140.        If clsDB.QueryExecuteNonQuery(strSQL2) = True Then
141.            '*** Condition Success ***'
142.        Else
143.            '*** Condition Error ***'
144.        End If
145.        clsDB.Close()
146. 
147.        '*** Delete ***'
148.        strSQL3 = "DELETE FROM customer WHERE CustomerID = 'C005' "
149.        If clsDB.QueryExecuteNonQuery(strSQL3) = True Then
150.            '*** Condition Success ***'
151.        Else
152.            '*** Condition Error ***'
153.        End If
154.        clsDB.Close()
155.    End Sub
156. 
157.    '*** Execute Transaction ***'
158.    Sub myExecuteTransaction()
159.        Dim strSQL1, strSQL2, strSQL3 As String
160. 
161.        '*** Start Transaction ***'
162.        clsDB.TransStart()
163. 
164.        Try
165.            '*** Insert ***'
166.            strSQL1 = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
167.            " VALUES('C005','Weerachai Nukitram','webmaster@thaicreate.com','TH','200000','100000')"
168.            clsDB.TransExecute(strSQL1) '*** Execute Query 1 ***'
169. 
170.            '*** Update ***'
171.            strSQL2 = "UPDATE customer SET Budget = '3000000' WHERE CustomerID = 'C005' "
172.            clsDB.TransExecute(strSQL2) '*** Execute Query 2 ***
173. 
174.            '*** Delete ***'
175.            strSQL3 = "DELETE FROM customer WHERE CustomerID = 'C005' "
176.            clsDB.TransExecute(strSQL3) '*** Execute Query 3 ***
177. 
178.            '*** Commit Transaction ***'
179.            clsDB.TransCommit()
180. 
181.        Catch ex As Exception
182.            '*** RollBack Transaction ***'
183.            clsDB.TransRollBack()
184.        End Try
185.        clsDB.Close()
186.    End Sub
187. 
188.End Class




   
Hate it
Don't like it
It's ok
Like it
Love it
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-30 07:45:22 / 2017-03-29 10:32:43
  Download : Download  ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)
 Sponsored Links / Related

 
ASP.NET SQL Server Connect to Database
Rating :

 
ASP.NET SQL Server List Table Properties
Rating :

 
ASP.NET SQL Server List Record
Rating :

 
ASP.NET SQL Server & OleDb (System.Data.OleDb)
Rating :

 
ASP.NET SQL Server & Odbc (System.Data.Odbc)
Rating :

 
ASP.NET SQL Server Random Record
Rating :

 
ASP.NET SQL Server List Record Paging/Pagination
Rating :

 
ASP.NET SQL Server Search Record
Rating :

 
ASP.NET SQL Server Search Record Paging/Pagination
Rating :

 
ASP.NET SQL Server Add/Insert Record
Rating :

 
ASP.NET SQL Server Add-Insert Multiple Record
Rating :

 
ASP.NET SQL Server Check Already Exists Add/Insert Record
Rating :

 
ASP.NET SQL Server Transaction (BeginTransaction,Commit,Rollback)
Rating :

 
ASP.NET SQL Server Edit/Update Record
Rating :

 
ASP.NET SQL Server Delete Record
Rating :

 
ASP.NET SQL Server Multiple Checkbox Delete Record
Rating :

 
ASP.NET SQL Server and GridView, DataSource
Rating :

 
ASP.NET SQL Server Database Class
Rating :

 
ASP.NET SQL Server Database Class (Visual Studio 2005,2008,2010 - .NET 2.0,3.5,4.0)
Rating :

 
ASP.NET SQL Server BLOB Binary Data and Parameterized Query
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่