Class to for common DB Functions

Posted by MF-13693 under VB.NET on 2/8/2012 | Points: 10 | Views : 1761 | Status : [Member] | Replies : 2
Hi to all,

I am coding a database access class to use for common database function like returning a sqldatareader based on the sql query provided.

Find below the code for it:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class DBLib

Private _constr As String

Public sqlDBUser_ As String
Public sqlDBPass_ As String
Public sqlDBName_ As String
Public sqlDBServer_ As String

Public Property ConnectionString() As String
Get
_constr = "Data Source=" & sqlDBServer_ & ";Initial Catalog=" & sqlDBName_ & ";User ID=" & sqlDBUser_ & ";Password=" & sqlDBPass_ & ";Integrated Security=false;"
Return _constr
End Get

Set(ByVal value As String)
_constr = value
End Set

End Property

Public Function TestSQLConnection() As Boolean
Dim retVal As String = False

Try
Dim con As New SqlConnection
con.ConnectionString = Me.ConnectionString
con.Open()
retVal = True
con.Close()
Catch ex As Exception
retVal = False
End Try

Return retVal

End Function


Public Function SelectRowsSQL(ByVal sSql As String) As SqlDataReader
Try
Dim con As New SqlConnection
con.ConnectionString = Me.ConnectionString
con.Open()

Dim dr As SqlDataReader
Dim cmd As New SqlCommand(sSql, con)
dr = cmd.ExecuteReader
con.Close()

Return dr
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try

End Function
End Class



Need you valuable suggestion regarding the best coding practices for the database access like performance and exception handlings.

Thx

M@F
Index was outside the bounds !!!



Responses

Posted by: Naraayanan on: 2/14/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Nice.. Is it work fine in your project?


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

MF-13693, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: MF-13693 on: 2/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Yes, working fine. Need some fine tuning to it !!! :-))

M@F
Index was outside the bounds !!!

MF-13693, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response