Advertisement
Guest User

Connect Access DB to Windows Form

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'هذا كود الاضافة
  2.  
  3.         If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Then
  4.             MsgBox("Please Enter All Informations...", MsgBoxStyle.Critical, Title:="Error")
  5.         Else
  6.             Try
  7.                 Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\Database.accdb;")
  8.                 Dim insert As String = "Insert into Users values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "');"
  9.                 Dim cmd As New OleDbCommand(insert, conn)
  10.                 conn.Open()
  11.                 cmd.ExecuteNonQuery()
  12.                 MsgBox("New User Added Successfully", MsgBoxStyle.Information, Title:="Add New User Sucess")
  13.                 Me.Close()
  14.             Catch ex As Exception
  15.                 MsgBox("Error", MsgBoxStyle.Critical, Title:="Error")
  16.             End Try
  17.         End If
  18.  
  19.  
  20.  
  21. 'Public Class Form1 وهذا الكود ضعه فوق كلمة
  22. Imports System.Data.OleDb
  23. '======================================================================================================================
  24. '======================================================================================================================
  25. '======================================================================================================================
  26. 'هذا كود التعديل
  27.  
  28.             Dim cmd As New OleDbCommand
  29.             Dim i As Integer
  30.             con.Open()
  31.             With cmd
  32.                 .Connection = con
  33.                 .CommandText = "UPDATE Sales SET ItemName = '" & ComboBox1.Text & "',ItemPrice = '" & TextBox1.Text & "',Quantity = '" & TextBox2.Text & "',Total = '" & TextBox3.Text & "',DateofSale = '" & TextBox4.Text & "'"
  34.                 i = .ExecuteNonQuery
  35.             End With
  36.             If i > 0 Then
  37.                 MsgBox("Update Record")
  38.             End If
  39.             con.Close()
  40.  
  41. 'End Sub هذا فنكشن حطه تحت اي كلمة
  42.  
  43.     Public Function jokendb() As OleDb.OleDbConnection
  44.         Return New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Database.accdb")
  45.     End Function
  46.  
  47. 'Public Class Form1 وهذا الكود حطه تحت كلمة
  48.  
  49. Public con As OleDb.OleDbConnection = jokendb()
  50. '======================================================================================================================
  51. '======================================================================================================================
  52. '======================================================================================================================
  53. 'هذا كود الحذف
  54.  
  55.         Dim con As OleDbConnection
  56.         Dim com As OleDbCommand
  57.         con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\Database.accdb")
  58.         com = New OleDbCommand("delete from Users where Username = @Username", con)
  59.         con.Open()
  60.         com.Parameters.AddWithValue("@Username", TextBox1.Text)
  61.         com.ExecuteNonQuery()
  62.         MsgBox("User Deleted Sucessfully.", MsgBoxStyle.Information, Title:="User Deleted")
  63.         con.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement