472,784 Members | 779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 4687
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.