Thursday, August 27, 2015

أكواد VB.NET لقواعد البيانات في الوضع المتصل والغير متصل

Add_Off :
Imports System.Data.OleDb
Public Class Form1

    Dim Con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & _
    Application.StartupPath & "\StudentBase.accdb") 'Application.StartupPath = path

    Dim Da As OleDbDataAdapter
    Dim Dt As New DataTable
    'Dim Cmd As OleDbCommand 'on Mode
    Dim Cmdb = New OleDbCommandBuilder
------------------------------------------
        Try
            Dim R As DataRow = Dt.NewRow
            R(0) = TextBox1.Text
            R(1) = TextBox2.Text
            R(2) = TextBox3.Text
            R(3) = TextBox4.Text
            R(4) = TextBox5.Text
            Dt.Rows.Add(R)
            Cmdb = New OleDbCommandBuilder(Da)
            Da.Update(Dt)
            MsgBox("Student added successfully !", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
        End Try

Add_On :
Imports System.Data.OleDb
Public Class Form1

    Dim Con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & _
    Application.StartupPath & "\StudentBase.accdb") 'Application.StartupPath = path

    'Dim Da As OleDbDataAdapter 'for DataGridView
    Dim Dt As New DataTable
  =>Dim Cmd As OleDbCommand

-------------------------------------------      

 Try
            Cmd = New OleDbCommand("insert into student values ('" & Me.TextBox1.Text _
                                   & "','" & Me.TextBox2.Text _
                                   & "','" & Me.TextBox3.Text _
                                   & "','" & Me.TextBox4.Text _
                                   & "','" & Me.TextBox5.Text _
                                   & "')", Con)
            Con.Open()
            Cmd.ExecuteNonQuery()
            Con.Close()
            'MsgBox("Student added successfully !", MsgBoxStyle.Information)
            MsgBox("Ajout reussi.")
            Dt.Clear()
            Form1_Load(sender, e)
        Catch ex As Exception
            'MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
            MsgBox("Erreur suivante rencontrée :" & ex.Message)

        End Try

Delet_Off :
        Dim Input As String
        Try
            Input = InputBox("Enter The ID Of Student TO Delete !", "Delete")

            For i As Integer = 0 To Dt.Rows.Count - 1
                If Input = Dt.Rows(i).Item(0) Then
                    Dt.Rows(i).Delete()
                    Cmdb = New OleDbCommandBuilder(Da)
                    Da.Update(Dt)
                    MsgBox("Student DeLeted Successfully",MsgBoxStyle.Information)
                    Exit Sub
                End If
            Next
            MsgBox("Not Found !")
        Catch ex As Exception
            MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
        End Try

Delet_On :
Imports System.Data.OleDb
Public Class Form1

    Dim Con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & _
    Application.StartupPath & "\StudentBase.accdb") 'Application.StartupPath = path

    Dim Da As OleDbDataAdapter
    Dim Dt As New DataTable
    Dim Cmd As OleDbCommand
    Dim Cmdb = New OleDbCommandBuilder

-----------
        Dim Input As String
        Try
            Input = InputBox("Enter The ID Of Student TO Delete !", "Delete")
            Cmd = New OleDbCommand("DELETE * FROM Student WHERE ID_Student=" & Input & "", Con)
            Con.Open()
            Cmd.ExecuteNonQuery()
            Con.Close()
            'MsgBox("Student Delete Successfully", MsgBoxStyle.Information)
            MsgBox("Stagiaire Supprimé.")
            Dt.Clear()
            Form1_Load(sender, e)
        Catch ex As Exception
            'MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
            MsgBox("Erreur suivante rencontrée :" & ex.Message)
        End Try

Edit_Off :
        Try
            For i = 0 To Dt.Rows.Count - 1
                If Dt.Rows(i).Item(0) = TextBox1.Text Then
                    Dt.Rows(i).Item(0) = TextBox1.Text
                    Dt.Rows(i).Item(1) = TextBox2.Text
                    Dt.Rows(i).Item(2) = TextBox3.Text
                    Dt.Rows(i).Item(3) = TextBox4.Text
                    Dt.Rows(i).Item(4) = TextBox5.Text
                    Cmdb = New OleDbCommandBuilder(Da)
                    Da.Update(Dt)
                    MsgBox("Student Edited Successfully", MsgBoxStyle.Information)
                End If
            Next
        Catch ex As Exception
            MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
        End Try

Edit_On:
'Click in button Find -> Edit      


 Try
            Cmd = New OleDbCommand("update Student set First_Name='" & _
                                   Me.TextBox2.Text & _
                                   "',Last_Name='" & _
                                   Me.TextBox3.Text & _
                                   "',Age='" &
                                   Me.TextBox4.Text & _
                                   "',Address='" &
                                   TextBox5.Text & _
                                   "' where ID_Student=" &
                                   TextBox1.Text & "", Con)
            Con.Open()
            Cmd.ExecuteNonQuery()
            Con.Close()
            'MsgBox("Student Edited successfully !", MsgBoxStyle.Information)
             MsgBox("Stagiaire modifié.")
            Dt.Clear()
            Form1_Load(sender, e)
        Catch ex As Exception
            'MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
             MsgBox("Erreur suivante rencontrée :" & ex.Message)

        End Try

Find_Off :
      Dim Input As String
        Try
            Input = InputBox("Enter ID of Student To Search !", "Search")

                For i As Integer = 0 To Dt.Rows.Count - 1
                    If Input = Dt.Rows(i).Item(0) Then
                    Me.TextBox1.Text = Dt.Rows(i).Item(0)
                    Me.TextBox2.Text = Dt.Rows(i).Item(1)
                    Me.TextBox3.Text = Dt.Rows(i).Item(2)
                    Me.TextBox4.Text = Dt.Rows(i).Item(3)
                    Me.TextBox5.Text = Dt.Rows(i).Item(4)
                        Exit Sub
                    End If
                Next
                MsgBox("Not Found !")
            Catch ex As Exception
                MsgBox("Some errors was occured !", MsgBoxStyle.Critical)
            End Try

Find_On :
Imports System.Data.OleDb
Public Class Form1

    Dim Con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & _
    Application.StartupPath & "\StudentBase.accdb") 'Application.StartupPath = path

    Dim Da As OleDbDataAdapter
    Dim Dt As New DataTable
    Dim Cmd As OleDbCommand
    Dim Cmdb = New OleDbCommandBuilder
    Dim Dr As OleDbDataReader ' search mode ON
---------------------------------------

        Dim Input As String
        Try
            Input = InputBox("Enter ID of Student To Search !", "Search")
            Cmd = New OleDbCommand("select * from student where ID_Student=" & Input & "", Con)
            Con.Open()
            Dr = Cmd.ExecuteReader
            While Dr.Read()
                Me.TextBox1.Text = Dr(0)
                Me.TextBox2.Text = Dr(1)
                Me.TextBox3.Text = Dr(2)
                Me.TextBox4.Text = Dr(3)
                Me.TextBox5.Text = Dr(4)
            End While
            Dr.Close()
            Con.Close()
        Catch ex As Exception
            'MsgBox("Some errors was occured !",MsgBoxStyle.Critical)
            MsgBox("Erreur suivante rencontrée :" & ex.Message)

        End Try

1 comment: