Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit On
  2.  
  3. Imports System.Data.OleDb
  4.  
  5. Public Class Form1
  6.     Dim DbConnect As OleDbConnection
  7.     Dim FileName As String
  8.  
  9.     Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click
  10.         'This is basically the same process as in the SQL Tester, however the command is specified in the code
  11.        'This is just to fill the DGV. If you got the SQL Tester done and understood it, this should be
  12.        'relatively straightforward
  13.        Dim ofd As OpenFileDialog
  14.         ofd = New OpenFileDialog
  15.         ofd.Filter = "Database Files|*.accdb"
  16.         ofd.ShowDialog()
  17.         FileName = ofd.FileName
  18.         Try
  19.             DbConnect = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & FileName)
  20.         Catch ex As Exception
  21.             MsgBox("Error: " & ex.Message)
  22.             Application.Exit()
  23.         End Try
  24.         Dim DBCommand As OleDbCommand = DbConnect.CreateCommand
  25.         DBCommand.CommandText = "SELECT * FROM Students"
  26.         DbConnect.Open()
  27.         Dim DBReader As OleDb.OleDbDataReader = DBCommand.ExecuteReader()
  28.         Dim numCols As Int16 = DBReader.FieldCount - 1
  29.         Dim strs(numCols) As String
  30.         Dim i As Int16
  31.         DGV.ColumnCount = numCols + 1
  32.         For i = 0 To numCols
  33.             strs(i) = New String("")
  34.             DGV.Columns(i).Name = DBReader.GetName(i)
  35.         Next
  36.         If DBReader.HasRows Then
  37.             While DBReader.Read()
  38.                 For i = 0 To numCols
  39.                     strs(i) = DBReader.GetValue(i).ToString()
  40.                 Next
  41.                 DGV.Rows.Add(strs)
  42.             End While
  43.         End If
  44.         DBReader.Close()
  45.         DbConnect.Close()
  46.         DBCommand.CommandText = "SELECT * FROM Majors"
  47.         DbConnect.Open()
  48.         DBReader = DBCommand.ExecuteReader()
  49.         If DBReader.HasRows Then
  50.             While DBReader.Read()
  51.                 cbMajor.Items.Add(DBReader.GetValue(2))
  52.             End While
  53.         End If
  54.     End Sub
  55.  
  56.     Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
  57.         Application.Exit()
  58.     End Sub
  59.  
  60.     Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  61.         'First we start of by establishing our connection with the databse
  62.        Try
  63.             DbConnect = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & FileName)
  64.         Catch ex As Exception
  65.             MsgBox("Error: " & ex.Message)
  66.             Application.Exit()
  67.         End Try
  68.         'Next we state the command we wish to use. In order to append to the database, we have
  69.        'to use INSERT INTO [Table] ([value1],...) VALUES ('[value1]',...)
  70.        'So Students (ID,...) tells it that we want to insert into student, and we should be
  71.        'sending the 6 values we have, and then we organize Values(...) the same way we did
  72.        'Students(...), but replacing the names with our values. It looks messy as hell in code
  73.        Dim DBCommand As OleDbCommand = DbConnect.CreateCommand
  74.         DBCommand.CommandText = "INSERT INTO Students (ID,First_Name,Last_Name,Major,GPA,Email)" _
  75.                                 & " VALUES ('" & txtID.Text & "','" & txtFname.Text & "','" _
  76.                                 & txtLname.Text & "','" & cbMajor.SelectedIndex + 3 & "','" _
  77.                                 & txtGPA.Text & "','" & txtFname.Text & "." & txtLname.Text _
  78.                                 & "@school.edu');"
  79.         'Open the connection, execute the command(inserting the data into the db), and then close
  80.        'the connection
  81.        DbConnect.Open()
  82.         Try
  83.             MsgBox(DBCommand.ExecuteNonQuery())
  84.         Catch ex As Exception
  85.             MsgBox("Error: " & ex.Message)
  86.         End Try
  87.         DbConnect.Close()
  88.         'Add the same information to the DGV that we sent to the database
  89.        If cbMajor.SelectedIndex > -1 Then
  90.             DGV.Rows.Add(txtID.Text, txtFname.Text, txtLname.Text, cbMajor.SelectedIndex, txtGPA.Text, _
  91.                                  txtFname.Text(1) + txtLname.Text.ToLower + "@school.edu")
  92.         End If
  93.     End Sub
  94. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement