Results 1 to 2 of 2

Thread: Generic Data Access Class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72

    Generic Data Access Class

    I'm new to VB.net, but have a strong background in VB6.

    I'm sarting on a new project that is going to have alot of data access to deal with. I want to make some generic reuseable components, especally for data access. How does this code look? It works to load a dataset and save changes/additions but is it good OO coding? I'm going to add properties to it to set the connection string, table, etc.

    VB Code:
    1. Public Class DBAccess
    2.  
    3.     Dim blnConnected As Boolean
    4.     Dim myConnString As String
    5.     Dim myConnection As OleDb.OleDbConnection
    6.     Dim myDataAdapter As OleDb.OleDbDataAdapter
    7.     Dim myCommand As OleDb.OleDbCommandBuilder
    8.     Dim myDataSet As New DataSet
    9.  
    10.    
    11.     Public Sub New(ByVal strConnection As String)
    12.  
    13.         myConnString = strConnection
    14.         myConnection = New OleDb.OleDbConnection(strConnection)
    15.  
    16.     End Sub
    17.  
    18.     Private Function f_Connect() As Boolean
    19.         Try
    20.             myConnection.Open()
    21.             blnConnected = True
    22.             Return True
    23.  
    24.         Catch ex As Exception
    25.             MsgBox(ex.ToString, MsgBoxStyle.OKOnly)
    26.             blnConnected = False
    27.             Return False
    28.  
    29.         End Try
    30.     End Function
    31.  
    32.     Private Function f_Disconnect() As Boolean
    33.         Try
    34.             myConnection.Close()
    35.             blnConnected = False
    36.             Return True
    37.  
    38.         Catch ex As Exception
    39.             MsgBox(ex.ToString, MsgBoxStyle.OKOnly)
    40.             blnConnected = True
    41.             Return False
    42.  
    43.         End Try
    44.  
    45.     End Function
    46.     Public Function f_GetDataset(ByVal strSqlStatement As String, ByVal strTable As String) As DataSet
    47.         ' Fill Dataset
    48.  
    49.         Try
    50.             f_Connect()
    51.             'Create a new instance of the data adapter and fill the dataset.
    52.             myDataAdapter = New OleDb.OleDbDataAdapter(strSqlStatement, myConnString)
    53.             myDataAdapter.Fill(myDataSet, strTable)
    54.  
    55.             'Create Update, Insert and Delete Commands
    56.             myCommand = New OleDb.OleDbCommandBuilder(myDataAdapter)
    57.             myDataAdapter.UpdateCommand = myCommand.GetUpdateCommand
    58.             myDataAdapter.InsertCommand = myCommand.GetInsertCommand
    59.             myDataAdapter.DeleteCommand = myCommand.GetDeleteCommand
    60.  
    61.             f_Disconnect()
    62.  
    63.             'Return a dataset
    64.             Return myDataSet
    65.  
    66.         Catch ex As Exception
    67.             MsgBox(ex.ToString)
    68.         End Try
    69.  
    70.  
    71.     End Function
    72.  
    73.  
    74.     Public Function f_UpdateDataSet(ByVal aDS As DataSet, ByVal strTable As String) As Boolean
    75.         Try
    76.             ' Update the data only if the dataset has changed.
    77.             If aDS.HasChanges Then
    78.                 f_Connect()
    79.                 myDataAdapter.Update(aDS, strTable)
    80.                 f_Disconnect()
    81.             End If
    82.  
    83.         Catch ex As Exception
    84.             Debug.Write(ex.ToString)
    85.         End Try
    86.  
    87.  
    88.  
    89.     End Function
    90.  
    91. End Class


    Thanks
    Chris Wilson

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    You should definitely check out Microsoft Data Access Application Block!

    http://msdn.microsoft.com/library/de...ml/daab-rm.asp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width