473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

generic database class in vb.net

I'm classic ASP developer and trying to switch to .net ...I'm newbie to ..net...
In classic ASP for ADO connectivity i used to put below code in one separate file and used to include that file whereever I need database opreation...

Dim objDatabase
Set objDatabase = Nothing
'-- Singleton for the CDatabase class
Function Database
If objDatabase Is Nothing Then
Set objDatabase = New CDatabase
End If
Set Database = objDatabase
End Function

'-- Begin CDatabase Class
Class CDatabase

Private m_objConn

Public Sub Class_Initialize()
Set m_objConn = Nothing
End Sub
Public Property Get Connection

'-- Reuse connection if one exist, create a new one otherwise
If m_objConn Is Nothing Then
Set m_objConn = Server.CreateObject("ADODB.Connection")

'On Error Resume Next

'm_objConn.Open Application.Contents.Item("ConnString")
m_objConn.Open SQL_CONNECTION

If Err.number <> 0 Then
Set m_objConn = Nothing
Response.Write "Error-->" & Err.description 'Application.Contents.Item("ErrorDatabaseConnect")
Response.End
End If
On Error GoTo 0
End If

Set Connection = m_objConn
End Property


Public Function ExecuteQuery (SqlQuery)
Dim objRs
Set objRs = Server.CreateObject("ADODB.Recordset")
objRs.Open SqlQuery, Me.Connection
If Err.number <> 0 Then
Set objRs = Nothing
Response.Write "Error-->" & Err.description 'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If
On Error GoTo 0
Set ExecuteQuery = objRs
End Function

Public Function ExecuteGetID (SqlQuery,tblName)
Dim intRecords
Dim objCmd
intRecords = -1
Set objCmd = Server.CreateObject("ADODB.Command")
'On Error Resume Next
objCmd.ActiveConnection = Me.Connection
objCmd.CommandText = SqlQuery
Call objCmd.Execute(intRecords)
If Err.number <> 0 Then
Set objCmd = Nothing
Response.Write "Error-->" & Err.description 'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If
Set objRs = Server.CreateObject("ADODB.Recordset")
Set objRs = Me.Connection.Execute("SELECT @@IDENTITY FROM " & tblName)
ExecuteGetID = objRs.fields(0).value
On Error GoTo 0
End Function

Public Function ExecuteCommand (SqlQuery)

Dim intRecords
Dim objCmd

intRecords = -1

'-- Create new command object
Set objCmd = Server.CreateObject("ADODB.Command")

On Error Resume Next

objCmd.ActiveConnection = Me.Connection
objCmd.CommandText = SqlQuery

Call objCmd.Execute(intRecords)

If Err.number <> 0 Then
Set objCmd = Nothing

Response.Write "Error-->" & Err.description 'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If

On Error GoTo 0

ExecuteCommand = intRecords
End Function
Public Function CreateRecordset (SqlQuery, CursorType, LockType)

Dim objRs

'-- Create new recordset object
Set objRs = Server.CreateObject("ADODB.Recordset")

On Error Resume Next

objRs.Open SqlQuery, Me.Connection, CursorType, LockType


If Err.number <> 0 Then
Set objRs = Nothing

Response.Write "Error-->" & Err.description 'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If

On Error GoTo 0
Set CreateRecordset = objRs
End Function

Public Function CreateRecordsetPage (SqlQuery, CursorType, LockType, iPageSize)

Dim objRs

'-- Create new recordset object
Set objRs = Server.CreateObject("ADODB.Recordset")
objRs.PageSize = iPageSize
objRs.CacheSize = iPageSize


On Error Resume Next

objRs.Open SqlQuery, Me.Connection, CursorType, LockType


If Err.number <> 0 Then
Set objRs = Nothing

Response.Write "Error-->" & Err.description 'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If

On Error GoTo 0
Set CreateRecordsetPage = objRs
End Function
Public Sub Class_Terminate()
Set m_objConn = Nothing
End Sub
End Class

I need similar kind of vb.net version...so whenever i need data, i just need to call class object...
I'm using sql server database and my connection string is stored in web.config file...
Can anyone help me to write class so I have same functionality...
Any help would be appreciated...
Thanx
dave

Nov 19 '05 #1
1 4718
Hi Dave,

Well, .Net is a totally different kind of animal from VBScript and ASP.Net,
so you can basically trash that code. For one thing, the .Net platform has
built-in Connection Pooling, which means that not only do you not need to
reuse a Connection object, it is in fact better to open and close them as
quickly as possible.

I had some very similar tools when I was doing ASP, and got into ASP.Net
very early on, so I didn't have some of the conveniences available to you
today, like the freely-downloadable .Net database application access block:

http://msdn.microsoft.com/library/de.../html/daab.asp

I would suggest not only using it, but studying it, for your own edification
and education. True Object-Oriented programming is a big meal to eat at one
sitting. Studying the code that Microsoft provides should help you get a
good handle on it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"dave" <fo****@foo.netREMOVE> wrote in message
news:eD******************@TK2MSFTNGP14.phx.gbl...
I'm classic ASP developer and trying to switch to .net ...I'm newbie to
..net...
In classic ASP for ADO connectivity i used to put below code in one separate
file and used to include that file whereever I need database opreation...

Dim objDatabase
Set objDatabase = Nothing
'-- Singleton for the CDatabase class
Function Database
If objDatabase Is Nothing Then
Set objDatabase = New CDatabase
End If
Set Database = objDatabase
End Function

'-- Begin CDatabase Class
Class CDatabase

Private m_objConn

Public Sub Class_Initialize()
Set m_objConn = Nothing
End Sub
Public Property Get Connection

'-- Reuse connection if one exist, create a new one otherwise
If m_objConn Is Nothing Then
Set m_objConn = Server.CreateObject("ADODB.Connection")

'On Error Resume Next

'm_objConn.Open Application.Contents.Item("ConnString")
m_objConn.Open SQL_CONNECTION

If Err.number <> 0 Then
Set m_objConn = Nothing
Response.Write "Error-->" & Err.description
'Application.Contents.Item("ErrorDatabaseConnect")
Response.End
End If
On Error GoTo 0
End If

Set Connection = m_objConn
End Property
Public Function ExecuteQuery (SqlQuery)
Dim objRs
Set objRs = Server.CreateObject("ADODB.Recordset")
objRs.Open SqlQuery, Me.Connection
If Err.number <> 0 Then
Set objRs = Nothing
Response.Write "Error-->" & Err.description
'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If
On Error GoTo 0
Set ExecuteQuery = objRs
End Function

Public Function ExecuteGetID (SqlQuery,tblName)
Dim intRecords
Dim objCmd
intRecords = -1
Set objCmd = Server.CreateObject("ADODB.Command")
'On Error Resume Next
objCmd.ActiveConnection = Me.Connection
objCmd.CommandText = SqlQuery
Call objCmd.Execute(intRecords)
If Err.number <> 0 Then
Set objCmd = Nothing
Response.Write "Error-->" & Err.description
'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If
Set objRs = Server.CreateObject("ADODB.Recordset")
Set objRs = Me.Connection.Execute("SELECT @@IDENTITY FROM " & tblName)
ExecuteGetID = objRs.fields(0).value
On Error GoTo 0
End Function

Public Function ExecuteCommand (SqlQuery)

Dim intRecords
Dim objCmd

intRecords = -1

'-- Create new command object
Set objCmd = Server.CreateObject("ADODB.Command")

On Error Resume Next

objCmd.ActiveConnection = Me.Connection
objCmd.CommandText = SqlQuery

Call objCmd.Execute(intRecords)

If Err.number <> 0 Then
Set objCmd = Nothing

Response.Write "Error-->" & Err.description
'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If

On Error GoTo 0

ExecuteCommand = intRecords
End Function
Public Function CreateRecordset (SqlQuery, CursorType, LockType)

Dim objRs

'-- Create new recordset object
Set objRs = Server.CreateObject("ADODB.Recordset")

On Error Resume Next

objRs.Open SqlQuery, Me.Connection, CursorType, LockType
If Err.number <> 0 Then
Set objRs = Nothing

Response.Write "Error-->" & Err.description
'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If

On Error GoTo 0
Set CreateRecordset = objRs
End Function

Public Function CreateRecordsetPage (SqlQuery, CursorType, LockType,
iPageSize)

Dim objRs

'-- Create new recordset object
Set objRs = Server.CreateObject("ADODB.Recordset")
objRs.PageSize = iPageSize
objRs.CacheSize = iPageSize
On Error Resume Next

objRs.Open SqlQuery, Me.Connection, CursorType, LockType
If Err.number <> 0 Then
Set objRs = Nothing

Response.Write "Error-->" & Err.description
'Application.Contents.Item("ErrorQueryExecute")
Response.End
End If

On Error GoTo 0
Set CreateRecordsetPage = objRs
End Function
Public Sub Class_Terminate()
Set m_objConn = Nothing
End Sub
End Class

I need similar kind of vb.net version...so whenever i need data, i just need
to call class object...
I'm using sql server database and my connection string is stored in
web.config file...
Can anyone help me to write class so I have same functionality...
Any help would be appreciated...
Thanx
dave
Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edward Diener | last post by:
Version 2.0 of the Python database API was written over 5 years ago, in 1999. While it has been used successfully by many implementations, there is no generic access into the data dictionary of...
3
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
3
by: .Net Newbie | last post by:
I'm new to .Net and need to create a generic (free) way to update lookup tables in SQL Server (using C#) in ASP.Net pages. I found an article at:...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
0
by: Mathieu Cartoixa | last post by:
Hi, I have a simple 2-tiers (client+database) application with simple Domain Model objects The Data Access Layer is abstracted via Data Mappers which use Data Transfer Objects to communicate...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
6
by: Milsnips | last post by:
Hi there, this is what i'm trying to achieve, i have separate identical classes for SqlClient, OracleClient, Odbc and OleDb, what i want is to create a Wrapper class that calls any of these 4...
4
by: mojeza | last post by:
I would like to create generic object which will be used for store of single row of DataTable. Lets say I create class as follow: Public Class Participant Public ParticipantID As Int64 Public...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
4
by: tadmill | last post by:
Hi, Is it possible for a generic list class to use Reflection on the generic type being used to detect a property within that type that refers to the same generic class, only with a different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.