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:
Public Class DBAccess Dim blnConnected As Boolean Dim myConnString As String Dim myConnection As OleDb.OleDbConnection Dim myDataAdapter As OleDb.OleDbDataAdapter Dim myCommand As OleDb.OleDbCommandBuilder Dim myDataSet As New DataSet Public Sub New(ByVal strConnection As String) myConnString = strConnection myConnection = New OleDb.OleDbConnection(strConnection) End Sub Private Function f_Connect() As Boolean Try myConnection.Open() blnConnected = True Return True Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.OKOnly) blnConnected = False Return False End Try End Function Private Function f_Disconnect() As Boolean Try myConnection.Close() blnConnected = False Return True Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.OKOnly) blnConnected = True Return False End Try End Function Public Function f_GetDataset(ByVal strSqlStatement As String, ByVal strTable As String) As DataSet ' Fill Dataset Try f_Connect() 'Create a new instance of the data adapter and fill the dataset. myDataAdapter = New OleDb.OleDbDataAdapter(strSqlStatement, myConnString) myDataAdapter.Fill(myDataSet, strTable) 'Create Update, Insert and Delete Commands myCommand = New OleDb.OleDbCommandBuilder(myDataAdapter) myDataAdapter.UpdateCommand = myCommand.GetUpdateCommand myDataAdapter.InsertCommand = myCommand.GetInsertCommand myDataAdapter.DeleteCommand = myCommand.GetDeleteCommand f_Disconnect() 'Return a dataset Return myDataSet Catch ex As Exception MsgBox(ex.ToString) End Try End Function Public Function f_UpdateDataSet(ByVal aDS As DataSet, ByVal strTable As String) As Boolean Try ' Update the data only if the dataset has changed. If aDS.HasChanges Then f_Connect() myDataAdapter.Update(aDS, strTable) f_Disconnect() End If Catch ex As Exception Debug.Write(ex.ToString) End Try End Function End Class
Thanks




Reply With Quote