Public Class dataLayer
Private Shared connection As OleDb.OleDbConnection
Private Shared updatableAdapter(1) As OleDb.OleDbDataAdapter
Private Shared transaction As OleDb.OleDbTransaction
Private Shared Sub connect()
connection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & IO.Path.Combine(My.Application.Info.DirectoryPath, "details.accdb") & ";Persist Security Info=False;")
connection.Open()
End Sub
Private Shared Sub disConnect()
connection.Close()
End Sub
Public Shared Function getJoinedTable() As DataTable
connect()
updatableAdapter(0) = New OleDb.OleDbDataAdapter("SELECT a.id, a.number, a.subjectName, a.years, a.birthYear, b.note FROM Table1 AS a INNER JOIN Table2 AS b ON a.id = b.id", connection)
updatableAdapter(1) = New OleDb.OleDbDataAdapter("SELECT * FROM Table2", connection) 'contains id, note
' Create the InsertCommand.
Dim command As New OleDb.OleDbCommand("INSERT INTO Table1 VALUES(@id, @number, @subjectName, @years, @birthYear)", connection)
' Add the parameters for the InsertCommand.
command.Parameters.Add("@id", OleDb.OleDbType.Integer, 5, "id")
command.Parameters.Add("@number", OleDb.OleDbType.VarChar, 50, "number")
command.Parameters.Add("@subjectName", OleDb.OleDbType.VarChar, 50, "subjectName")
command.Parameters.Add("@years", OleDb.OleDbType.Integer, 3, "years")
command.Parameters.Add("@birthYear", OleDb.OleDbType.Integer, 4, "birthYear")
'command.Parameters.Add("@note", OleDb.OleDbType.VarChar, 100, "note")
updatableAdapter(0).InsertCommand = command
command = New OleDb.OleDbCommand("INSERT INTO Table2 VALUES(@id, @note)", connection)
' Add the parameters for the InsertCommand.
command.Parameters.Add("@id", OleDb.OleDbType.Integer, 5, "id")
command.Parameters.Add("@note", OleDb.OleDbType.VarChar, 100, "note")
updatableAdapter(1).InsertCommand = command
' Create the UpdateCommand.
command = New OleDb.OleDbCommand( _
"UPDATE Table1 SET id = @id, [number] = @number, subjectName = @subjectName, years = @years, " & _
"birthYear = @birthYear WHERE id = @oldID", connection)
' Add the parameters for the UpdateCommand.
command.Parameters.Add("@id", OleDb.OleDbType.Integer, 5, "id")
command.Parameters.Add("@number", OleDb.OleDbType.VarChar, 50, "number")
command.Parameters.Add("@subjectName", OleDb.OleDbType.VarChar, 50, "subjectName")
command.Parameters.Add("@years", OleDb.OleDbType.Integer, 3, "years")
command.Parameters.Add("@birthYear", OleDb.OleDbType.Integer, 4, "birthYear")
Dim parameter As OleDb.OleDbParameter = command.Parameters.Add( _
"@oldID", OleDb.OleDbType.Integer, 5, "id")
parameter.SourceVersion = DataRowVersion.Original
updatableAdapter(0).UpdateCommand = command
' Create the UpdateCommand.
command = New OleDb.OleDbCommand( _
"UPDATE Table2 SET id = @idnote = @note WHERE id = @oldID", connection)
' Add the parameters for the UpdateCommand.
command.Parameters.Add("@id", OleDb.OleDbType.Integer, 5, "id")
command.Parameters.Add("@note", OleDb.OleDbType.VarChar, 100, "note")
parameter = command.Parameters.Add( _
"@oldID", OleDb.OleDbType.Integer, 5, "id")
parameter.SourceVersion = DataRowVersion.Original
updatableAdapter(1).UpdateCommand = command
' Create the DeleteCommand.
command = New OleDb.OleDbCommand( _
"DELETE FROM Table1 WHERE id = @id", connection)
' Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add( _
"@id", OleDb.OleDbType.Integer, 5, "id")
parameter.SourceVersion = DataRowVersion.Original
updatableAdapter(0).DeleteCommand = command
' Create the DeleteCommand.
command = New OleDb.OleDbCommand( _
"DELETE FROM Table2 WHERE id = @id", connection)
' Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add( _
"@id", OleDb.OleDbType.Integer, 5, "id")
parameter.SourceVersion = DataRowVersion.Original
updatableAdapter(1).DeleteCommand = command
Dim dt As New DataTable
updatableAdapter(0).Fill(dt)
Return dt
End Function
Public Shared Sub update(ByVal dt As DataTable, ByVal adapterIndex As Integer)
updatableAdapter(adapterIndex).Update(dt)
connection.Close()
End Sub
End Class