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 !!!