简体   繁体   中英

Optimize this Database Code (Vb.Net)

So I have code that runs in a Vb.Net project that reads specific cols from a table in a Access Database (2007). It then populates a Datagridview with the results. The Issue is the code is messy, I'm disposing of all the events...etc. Is there a way to optimize it? From what I read, I can use the command "using" but I'm having problems trying to implement it. Any help on cleaning up this code is associated.

Sub Populate_RecordsList_Via_Database()

    'Create a connection to the database
    Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_Info.accdb;"
    Dim objConnection As New OleDbConnection(strConnection)

    'Open the connection with error handling
    Try
        objConnection.Open()
    Catch OleDbExceptionErr As OleDbException
        MessageBox.Show(OleDbExceptionErr.Message)
    Catch InvalidOperationErr As InvalidOperationException
        MessageBox.Show(InvalidOperationErr.Message)
    End Try

    'Create a command object with the SQL statement needed to select the first and last names
    Dim strSQL As String = "SELECT [RecordID], [FName], [LName], [SBAlias1] FROM [Records];"
    Dim objCommand As New OleDbCommand(strSQL, objConnection)

    'Create a data adapter and data table then fill the data table
    Dim objDataAdapter As New OleDbDataAdapter(objCommand)
    Dim objDataTable As New DataTable("Info")
    objDataAdapter.Fill(objDataTable)

    'close connection and release resources
    objConnection.Close()
    objConnection.Dispose()
    objConnection = Nothing
    objCommand.Dispose()
    objCommand = Nothing
    objDataAdapter.Dispose()
    objDataAdapter = Nothing

    'Populate datagridview
    For Each row As DataRow In objDataTable.Rows

        Dim n As Integer = DGV_ListView.Rows.Add()
        DGV_ListView.Rows.Item(n).Cells(0).Value = row.Item("RecordID")
        DGV_ListView.Rows.Item(n).Cells(1).Value = row.Item("FName")
        DGV_ListView.Rows.Item(n).Cells(2).Value = row.Item("LName")
        DGV_ListView.Rows.Item(n).Cells(3).Value = row.Item("SBAlias1")

    Next

    'Release resources
    objDataTable.Dispose()
    objDataTable = Nothing


End Sub

The using statement is very useful around disposable objects. This means that every class implementing the IDisposable interface is supposed to free the unmanaged resources acquired during their lifetime.

However in ADO.NET there is no unmanaged resource and thus is not really necessary to Dispose the OleDbDataAdapter and the DataTable while it is very recommended around the classes derived by the DbConnection base class.

So your revised code could be something like this

Sub Populate_RecordsList_Via_Database()

    Dim strSQL As String = "SELECT [RecordID], [FName], [LName], [SBAlias1] FROM [Records];"
    Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_Info.accdb;"
    Try
        Using objConnection = New OleDbConnection(strConnection)
        Using objCommand = New OleDbCommand(strSQL, objConnection)
        'Using objDataAdapter = New OleDbDataAdapter(objCommand)
            Dim objDataTable As New DataTable("Info")
            objDataAdapter.Fill(objDataTable)
            ' No need to loop over the datatable, just assign it to the DataSource property
            DGV_ListView.DataSource = objDataTable
        'End Using
        End Using
        End Using
    Catch OleDbExceptionErr As OleDbException
        MessageBox.Show(OleDbExceptionErr.Message)
    Catch InvalidOperationErr As InvalidOperationException
        MessageBox.Show(InvalidOperationErr.Message)
    End Try
End Sub

Notice the try/catch encloses everything and not only the opening of the connection because you can never tell where an exceptional situation could arise in this context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM