Imports Microsoft.VisualBasic Imports System.Data.OleDb Public Class DbControl 'Create new database connection Private DBCon As New OleDbConnection("Provider = microsoft.ACE.oledb.12.0;" & "Data source=FreightMaster.accdb;") 'Prepare DBCmd as OLEDB Private DBCmd As OleDb.OleDbCommand 'Preparing data Public dbda As oledbdataadapter Public DBDT As DataTable 'Query Parameters Public Params As New List(Of OleDb.OleDbParameter) 'qry statistics Public RecordCount As Integer Public Exception As String Public Sub execQuery(ByVal Query As String) 'Reset query stats RecordCount = 0 Exception = "" Try 'try to open the connection DBCOn.open() 'create the command DBCmd = New OleDbCommand(Query, DBCon) ' Load Params into dbcommand Params.ForEach(Sub(p) DBCmd.Parameters.Add(p)) 'clear the parameters params.clear() 'execute command DBDT = New datatable DBDA = New oledbdataAdapter(dbcmd) RecordCount = dbda.Fill(DBDT) Catch ex As Exception Exception = ex.message MessageBox.Show(Exception) End Try 'close the connection if it remains open If DBCon.State = ConnectionState.Open Then DBCon.Close() End Sub 'Include query and cmd params Public Sub AddParam(ByVal Name As String, ByVal value As Object) Dim NewParam As New OleDbParameter(Name, value) Params.Add(NewParam) End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)