I'm fairly new to the whole object oriantation thing and as such I've been mostly been using it as if it were not; (the fact that the whole project is developed by me doesn't help). But as I'm getting more into it, and understanding it, I guess I'm getting to the point of actually creating my classes to make something I can re-use across multiple projects.
I've realised that even thou I do some damn complex select statements, every single database query I've done the past 3 years (vb.net or vb6) were the big 4 !
Therefore, nothing more natural than a nice sweet smooth easy to use Class to do it for me.
So this thread is only to share my Class with you guys, re-use it at your will, and for the more experienced ones, please.. tips, suggestions of changes, criticisms, ... all welcome!
I've tried to do it nicely comment, including those nice pop-ups when you're typing to use its methods or properties.
It's basically 2 methods and 2 properties, and the constructor
- you have to pass the connection string to the constructor
- Query method to send a SELECT statement to the database, writes the returned values in the provided datatable and returns True or False for success/fail
- InsertUpdateDelete method that (as the name say) executes any of those non-query statements and return True or False for success/fail
- ErrorMessage property that holds the error message from any failed method
- NumberOfRowsAffected property that holds the number of rows affected or returned by the last method
I guess it doesn't get simpler than that, if it is what you need .
usage example:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
and here is my DB class:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
		
			
		
		
	
				
			I've realised that even thou I do some damn complex select statements, every single database query I've done the past 3 years (vb.net or vb6) were the big 4 !
Therefore, nothing more natural than a nice sweet smooth easy to use Class to do it for me.
So this thread is only to share my Class with you guys, re-use it at your will, and for the more experienced ones, please.. tips, suggestions of changes, criticisms, ... all welcome!
I've tried to do it nicely comment, including those nice pop-ups when you're typing to use its methods or properties.
It's basically 2 methods and 2 properties, and the constructor
- you have to pass the connection string to the constructor
- Query method to send a SELECT statement to the database, writes the returned values in the provided datatable and returns True or False for success/fail
- InsertUpdateDelete method that (as the name say) executes any of those non-query statements and return True or False for success/fail
- ErrorMessage property that holds the error message from any failed method
- NumberOfRowsAffected property that holds the number of rows affected or returned by the last method
I guess it doesn't get simpler than that, if it is what you need .
usage example:
			
				VB.NET:
			
		
		
		        Dim TestDB As New DB("Password=ui;Persist Security Info=True;User ID=ui;Initial Catalog=MyInitialCatalog;Data Source=MyServerName")
        If TestDB.InsertUpdateDelete("INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3)") Then
            MessageBox.Show(TestDB.NumberOfRowsAffected & " rows were modified")
        Else
            MessageBox.Show(TestDB.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Dim DT As New DataTable
        If TestDB.Query("SELECT * FROM table_name", DT) Then
            MessageBox.Show(TestDB.NumberOfRowsAffected & " rows were retrieved")
        Else
            MessageBox.Show(TestDB.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Ifand here is my DB class:
			
				VB.NET:
			
		
		
		Option Explicit On
Imports System.Data.SqlClient
''' <summary>
''' Provides easy to use functions to perform the 4 basic SQL operations (SELECT, INSERT, UPDATE, DELETE). Create by Budius 2010.
''' </summary>
''' <remarks></remarks>
Public Class DB
    Private ConnStr As String
    Private ErrMessage As String = ""
    Private AffectedRows As Integer = 0
    ''' <summary>
    ''' Gets the message from the last error
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>It will return "" no errors happen</remarks>
    Public ReadOnly Property ErrorMessage As String
        Get
            Return ErrMessage
        End Get
    End Property
    ''' <summary>
    ''' Gets the number of rows affected by the last query
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>It will return -1 case the last query had errors</remarks>
    Public ReadOnly Property NumberOfRowsAffected As Integer
        Get
            Return AffectedRows
        End Get
    End Property
    ''' <summary>
    ''' </summary>
    ''' <param name="ConnectionString">Connection String to the SQL server (e.g. User ID=ui;Password=ui;Persist Security Info=True;Initial Catalog=MyInitialCatalog;Data Source=MyDataServerName)</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal ConnectionString As String)
        ConnStr = ConnectionString
    End Sub
    ''' <summary>
    ''' Query the database and place the result into the provided datatable
    ''' </summary>
    ''' <param name="QueryStr">SQL query string</param>
    ''' <param name="DT">Datatable to receive the result</param>
    ''' <returns>Returns the number of rows returned by the query</returns>
    ''' <remarks>Returns -1 case an error occurs</remarks>
    Function Query(ByVal QueryStr As String, ByRef DT As DataTable) As Boolean
        AffectedRows = -1
        Try
            Dim Conn As New SqlConnection
            Conn.ConnectionString = ConnStr
            Dim Adapter As New SqlDataAdapter(QueryStr, ConnStr)
            ' Clear the table
            DT.Clear()
            DT.Reset()
            ' Opens the connection
            Conn.Open()
            ' Execute the Query
            AffectedRows = Adapter.Fill(DT)
            ' Closes connection
            Conn.Close()
            ' Dispose from resources
            Adapter.Dispose()
            Conn.Dispose()
        Catch ex As Exception
            ErrMessage = ex.Message
            'MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        If AffectedRows = -1 Then
            Query = False
        Else
            Query = True
        End If
    End Function
    ''' <summary>
    ''' Inserts, updates or deletes values onto/from the database using the SQL statement provided and return the number of rows affected.
    ''' </summary>
    ''' <param name="SQLStatement">INSERT, UPDATE or DELETE SQL statement to be executed onto the database</param>
    ''' <returns>Returns the number of rows affected by the SQL statement</returns>
    ''' <remarks>Returns -1 case an error occurs</remarks>
    Function InsertUpdateDelete(ByVal SQLStatement As String) As Integer
        AffectedRows = -1
        Try
            Dim Conn As New SqlConnection
            Conn.ConnectionString = ConnStr
            Dim Cmd As New SqlCommand(SQLStatement, Conn)
            Cmd.Connection.Open()
            AffectedRows = Cmd.ExecuteNonQuery()
            Cmd.Connection.Close()
            Cmd.Dispose()
            Conn.Dispose()
        Catch ex As Exception
            ErrMessage = ex.Message
            'MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        If AffectedRows = -1 Then
            InsertUpdateDelete = False
        Else
            InsertUpdateDelete = True
        End If
    End Function
End Class 
	 
 
		 
 
		
 
 
		 
 
		 
 
		