Wilson Kutegeka

Microsoft MVP - Visual Basic www.clinicmaster.net

Using List<T> Generic in your data layer

 

List<T> is basically a better ArrayList. It is optimized for speed, size, and power. Use it for majority of internal implementations whenever you need to store items in a container. This example shows you how you can use a List<T> generic in VB .NET to Save data into the database wrapped in a transaction in your Data access Layer  Explanation 
  • Create a base object say DBConnect that other object to save data to the database will derive from
  • Create a SaveAll method that allow an object of type DBConnect to save data in an optional transactional manner
  • Create an object such as Clients that derives from DBConnect
  • Define a list of Clients objects and call SaveAll to process them as a transaction
 

See code below

 

  

Imports System.Data

Imports System.Data.SqlClient

Imports System.Collections.Generic

Public Class frmClients

Private Sub btnSaveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAll.Click

Dim oClients As New Clients Dim lClients As New List(Of Clients)

Dim records As Integer

Dim dt As New DataTable

Try

Me.Cursor = Cursors.WaitCursor

 

dt.Columns.Add(
"ClientID", GetType(String))

dt.Columns.Add("FirstName", GetType(String))

dt.Columns.Add("LastName", GetType(String))

dt.Rows.Add(New Object() {"001", "Wilson", "Kutegeka"})

dt.Rows.Add(New Object() {"002", "John", "Magezi"})

dt.Rows.Add(New Object() {"003", "Peter", "Okello"})

dt.Rows.Add(New Object() {"004", "Jane", "Miranda"})

For pos As Integer = 0 To dt.Rows.Count - 1

oClients = New Clients With dt.Rows(pos)

If IsDBNull(.Item("ClientID")) Then

oClients.ClientID = String.Empty Else : oClients.ClientID = CStr(.Item("ClientID"))

End If

If IsDBNull(.Item("FirstName")) Then

oClients.FirstName = String.Empty Else : oClients.FirstName = CStr(.Item("FirstName"))

End If

If IsDBNull(.Item("LastName")) Then

oClients.LastName = String.Empty Else : oClients.LastName = CStr(.Item("LastName"))

End If

End With

lClients.Add(oClients)

Next

records = SaveAll(lClients, True)

MessageBox.Show(records & " records inserted!")

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

Me.Cursor = Cursors.Default

End Try

End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

Me.Close()

End Sub

End Class

#Region " Objects "

Public MustInherit Class DBConnect

#Region " Fields "

#End Region

#Region " Properties "

#End Region

#Region " Methods "

Friend Overridable Function SaveCommand() As SqlCommand

Return Nothing

End Function

Friend Overridable Function UpdateCommand() As SqlCommand

Return Nothing

End Function

Friend Overridable Function DeleteCommand() As SqlCommand

Return Nothing

End Function

#End Region

End Class

Public Class Clients : Inherits DBConnect

#Region " Fields "

Private m_ClientID As String

Private m_FirstName As String

Private m_LastName As String

#End Region

#Region " Properties "

Public Property ClientID() As String

Get

Return m_ClientID

End Get

Set(ByVal Value As String)

m_ClientID = Value

End Set

End Property

Public Property FirstName() As String

Get

Return m_FirstName

End Get

Set(ByVal Value As String)

m_FirstName = Value

End Set

End Property

Public Property LastName() As String

Get

Return m_LastName

End Get

Set(ByVal Value As String)

m_LastName = Value

End Set

End Property

#End Region

#Region " Methods "

Friend Overrides Function SaveCommand() As SqlCommandUsing comm As SqlCommand = New SqlCommand("uspInsertClients")

comm.CommandType = CommandType.StoredProcedure

With comm.Parameters

.AddWithValue("@ClientID", Me.ClientID)

.AddWithValue("@FirstName", Me.FirstName)

.AddWithValue("@LastName", Me.LastName)

End With

Return comm

End Using

End Function

#End Region

End Class

Public Module modMain

''' <summary>

''' Construct a connection string in a way that eliminates SQL injection

''' </summary>

''' <param name="_ConString"></param>

''' <returns></returns>

''' <remarks></remarks>

Public Function ConString(Optional ByVal _ConString As String = Nothing) As String

Dim serverName As String

Dim databaseName As String

Dim conBuilder As New SqlConnectionStringBuilder

serverName = "(local)"

databaseName = "Accounting"

If _ConString = Nothing OrElse _ConString = String.Empty Then

conBuilder.Clear()

conBuilder(
"Server") = serverName

conBuilder.InitialCatalog = databaseName

conBuilder("Integrated Security") = "SSPI"

conBuilder.PersistSecurityInfo = False

Return conBuilder.ConnectionString Else : Return _ConString

End If

End Function

''' <summary>

''' Set IsTransaction to true if you want all data to save or all to fail

''' </summary>

''' <typeparam name="T"></typeparam>

''' <param name="_List"></param>

''' <param name="IsTransaction"></param>

''' <returns></returns>

''' <remarks></remarks>

'''

Public Function SaveAll(Of T As DBConnect)(ByRef _List As List(Of T), Optional ByVal IsTransaction As Boolean = False) As Integer

Dim records As Integer

Dim comm As SqlCommand

Dim tran As SqlTransaction = Nothing

Using conn As New SqlConnection

Try

conn.ConnectionString = ConString()

conn.Open()

If IsTransaction = True Then

tran = conn.BeginTransaction()

For Each list As T In _List

Try

comm = list.SaveCommand()

comm.Connection = conn

comm.Transaction = tran

If comm.ExecuteNonQuery > 0 Then records += 1Catch ex As Exception

tran.Rollback()

Throw New ArgumentException("Transaction was canceled!" & vbCrLf & ex.Message)

End Try

Next

tran.Commit()

ElseIf IsTransaction = False Then

For Each list As T In _List

Try

comm = list.SaveCommand()

comm.Connection = conn

If comm.ExecuteNonQuery > 0 Then records += 1Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

Next

End If

Return records

Catch eX As SqlException

Throw eX

Catch eX As Exception

Throw eX

Finally

conn.Close()

End Try

End Using

End Function

End Module

#End Region

 

 

 

Comments

Mischa Kroon said:

This is quite a bit of code to see without indenting it.

Can you have a try at that or maybe make a screenshot + download link which would also make it easier to see.

# February 27, 2008 11:47 AM

Dennis van der Stelt said:

True... Use either SourceCodeForHtml (search here on BloggingAbout.NET for it for more info) and supply a solution with it.

You can either attach it to your post or upload it into a file category on this site.

# February 28, 2008 10:35 AM

Wilson Kutegeka said:

sorry for this but I am going to re-send it soon

# April 21, 2009 2:35 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 5 and 2 and type the answer here: