CC105Notes
CC105Notes
OleDb
Module Module1
Public connstr As String = "Provider=Microsoft.Jet.Oledb.4.0; data
source =" & Application.StartupPath & "\studentdb.mdb"
Public conn As New OleDbConnection(connstr)
//this is our module on the previous lesson
Function connect()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Return True
End Function
End Module
This is for the form and the global variables to be used
Imports System.Data.OleDb
Public Class Form1
Dim da As New OleDbDataAdapter
Dim dset As New DataSet
Dim Comm As OleDbCommand
Dim code As String
End Class
//this is the code to used for the base load of form 1.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
populate()
da = New OleDbDataAdapter("Select * from Studentcourse", conn)
dset = New DataSet
da.Fill(dset, "Studentcourse")
Dim count As Integer = dset.Tables("Studentcourse").Rows.Count
Dim x As Integer
For x = 0 To count - 1
CBOCOURSE.Items.Add(dset.Tables("Studentcourse").Rows(x).Item("Cd
es"))
Next
End Sub
//In this you can select the coursecode by selecting course description
//the function populate is used in form load
Function populate()
da = New OleDbDataAdapter("Select * from Studentdata", conn)
dset = New DataSet
da.Fill(dset, "Studentdata")
dgv.DataSource = dset.Tables("Studentdata").DefaultView
Return True
End Function
//finally the command to add a record to the database
Private Sub BADD_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BADD.Click
code = dset.Tables("Studentcourse").Rows(0).Item("Ccode")
connect()
Comm = New OleDbCommand
Comm.Connection = conn
Comm.CommandText = "Insert into Studentdata values('" &
TXTID.Text & "', '" & TXTFNAME.Text & "', '" & TXTLNAME.Text & "', '" &
code & "', '" & CBOSEC.Text & "')"
Comm.ExecuteNonQuery() //Executes a statement that does not
return a result set, such as an INSERT, UPDATE, DELETE, or data
definition statement.
MsgBox("Succesfully Added into Database")
populate()
End Sub