Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class db_operation
'The following example uses the OleDbCommand, along
'OleDbDataAdapter and OleDbConnection, to select rows from an Access database.
'The filled DataSet is then returned.
'The example return a DataSet,
'and is passed a query string that is an SQL SELECT statement,
'and a string that is the name of the source database table
'a connection string which is global variable stored in Connection_String Module
Function PopulateDataset(ByVal str_sqa As String, ByVal TableName As String) As dataset
Try
Dim con As OleDbConnection 'Represents an open connection to a data source
'The OleDbDataAdapter provides this bridge by using Fill
'to load data from the data source into the DataSet
Dim da As OleDbDataAdapter
Dim ds As dataset
con = New OleDbConnection(ConnString)
da = New OleDbDataAdapter(str_sqa, con)
ds = New dataset
da.Fill(ds, TableName)
Return ds
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
'This sub routine handles database operation such as add. delete, or update
Sub db_Operation(ByVal src_sqa As String, ByVal msg As String)
Try
Dim con As New OleDbConnection(ConnString)
Dim str_sqa As String
'OleDbCommand - Represents an SQL statement
'or stored procedure to execute against a data source.
Dim cmd As New OleDbCommand(src_sqa, con)
con.Open()
str_sqa = src_sqa
'ExecuteNonQuery
'Executes a SQL statement against the Connection
'and returns the number of rows affected.
cmd.ExecuteNonQuery()
con.Close()
MsgBox(msg)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Function GetFilledTable(ByVal query As String, ByVal connection As OleDbConnection) As DataTable
Dim command As New OleDbCommand(query, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.KeyInfo Or CommandBehavior.CloseConnection)
Dim schema As DataTable = reader.GetSchemaTable()
Dim columns(schema.Rows.Count - 1) As DataColumn
Dim column As DataColumn
'Build the schema for the table that will contain the data.
For i As Integer = 0 To columns.GetUpperBound(0) Step 1
column = New DataColumn
column.AllowDBNull = CBool(schema.Rows(i)("AllowDBNull"))
column.AutoIncrement = CBool(schema.Rows(i)("IsAutoIncrement"))
column.ColumnName = CStr(schema.Rows(i)("ColumnName"))
column.DataType = CType(schema.Rows(i)("DataType"), Type)
If column.DataType Is GetType(String) Then
column.MaxLength = CInt(schema.Rows(i)("ColumnSize"))
End If
column.ReadOnly = CBool(schema.Rows(i)("IsReadOnly"))
column.Unique = CBool(schema.Rows(i)("IsUnique"))
columns(i) = column
Next i
Dim data As New DataTable
Dim row As DataRow
data.Columns.AddRange(columns)
'Get the data itself.
While reader.Read()
row = data.NewRow()
For i As Integer = 0 To columns.GetUpperBound(0)
row(i) = reader(i)
Next i
data.Rows.Add(row)
End While
reader.Close()
Return data
End Function
End Class