syad28

Connecting Microsoft Access Database with VisualBasic

Mar 13th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class Form1
  2.     Dim con As New OleDb.OleDbConnection
  3.     Dim dbProvider As String
  4.     Dim dbSource As String
  5.     Dim sql As String
  6.     Dim da As OleDb.OleDbDataAdapter
  7.     Dim listitem As ListViewItem
  8.    
  9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
  11.         dbSource = "Data Source = |DataDirectory|/Data.mdb;Persist Security Info=True"
  12.         con.ConnectionString = dbProvider & dbSource
  13.  
  14.         Dim Header As ColumnHeader
  15.         ListView1.Width = 405
  16.         Header = New ColumnHeader
  17.         Header.Text = "No"
  18.         Header.TextAlign = HorizontalAlignment.Center
  19.         Header.Width = 50
  20.         ListView1.Columns.Add(Header)
  21.  
  22.         Header = New ColumnHeader
  23.         Header.Text = "Name"
  24.         Header.TextAlign = HorizontalAlignment.Center
  25.         Header.Width = 150
  26.         ListView1.Columns.Add(Header)
  27.  
  28.         Header = New ColumnHeader
  29.         Header.Text = "City"
  30.         Header.TextAlign = HorizontalAlignment.Center
  31.         Header.Width = 100
  32.         ListView1.Columns.Add(Header)
  33.  
  34.         Header = New ColumnHeader
  35.         Header.Text = "Phone"
  36.         Header.TextAlign = HorizontalAlignment.Center
  37.         Header.Width = 100
  38.         ListView1.Columns.Add(Header)
  39.         Display()
  40.     End Sub
  41.  
  42.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  43.         If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
  44.             MsgBox("Please Fill in the blank")
  45.         Else
  46.             con.Open()
  47.             sql = "insert into Table1(PersonName,City,Phone) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"
  48.             Dim cmd As New OleDb.OleDbCommand(sql, con)
  49.             cmd.CommandText = sql
  50.             cmd.Connection = con
  51.             cmd.ExecuteNonQuery()
  52.             con.Close()
  53.             Display()
  54.             TextBox1.Text = ""
  55.             TextBox2.Text = ""
  56.             TextBox3.Text = ""
  57.         End If
  58.     End Sub
  59.  
  60.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  61.         If TextBox1.Text <> "" Then
  62.             con.Open()
  63.             sql = "update Table1 set City='" & TextBox2.Text & "',Phone='" & TextBox3.Text & "' where PersonName='" & TextBox1.Text & "'"
  64.             Dim cmd As New OleDb.OleDbCommand(sql, con)
  65.             cmd.CommandText = sql
  66.             cmd.Connection = con
  67.             cmd.ExecuteNonQuery()
  68.             con.Close()
  69.             Display()
  70.             TextBox1.Text = ""
  71.             TextBox2.Text = ""
  72.             TextBox3.Text = ""
  73.         Else
  74.             MsgBox("Please Fill Name")
  75.         End If
  76.     End Sub
  77.  
  78.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  79.         If TextBox1.Text <> "" Then
  80.             con.Open()
  81.             sql = "delete from Table1 where PersonName='" & TextBox1.Text & "'"
  82.             Dim cmd As New OleDb.OleDbCommand(sql, con)
  83.             cmd.CommandText = sql
  84.             cmd.Connection = con
  85.             cmd.ExecuteNonQuery()
  86.             con.Close()
  87.             Display()
  88.             TextBox1.Text = ""
  89.             TextBox2.Text = ""
  90.             TextBox3.Text = ""
  91.         Else
  92.             MsgBox("Please Fill Name")
  93.         End If
  94.     End Sub
  95.  
  96.     Private Sub Display()
  97.         ListView1.Items.Clear()
  98.         Dim MyTable As New DataTable
  99.         con.Open()
  100.         sql = "select *from Table1"
  101.         da = New OleDb.OleDbDataAdapter(sql, con)
  102.         da.Fill(MyTable)
  103.         For i = 0 To MyTable.Rows.Count - 1
  104.             listitem = ListView1.Items.Add((ListView1.Items.Count).ToString() + 1)
  105.             listitem.SubItems.Add(MyTable.Rows(i)("PersonName"))
  106.             listitem.SubItems.Add(MyTable.Rows(i)("City"))
  107.             listitem.SubItems.Add(MyTable.Rows(i)("Phone"))
  108.         Next
  109.         con.Close()
  110.     End Sub
  111. End Class
Advertisement
Add Comment
Please, Sign In to add comment