⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 vb和数据库access的链接
💻 VB
📖 第 1 页 / 共 3 页
字号:

            Dim MyOledbCommand As OleDbCommand = New OleDbCommand()


            Dim String1, String2, String3, String4, String5 As String

            String1 = TextBox1.Text
            String2 = TextBox2.Text
            String3 = TextBox3.Text
            String4 = TextBox4.Text
            String5 = TextBox5.Text



            MyOledbCommand.CommandText = "Update Table1 Set " _
            & "Column1='" & String1 & "', " _
            & "Column2='" & String2 & "', " _
            & "Column3='" & String3 & "', " _
            & "Column4='" & String4 & "', " _
            & "Column5='" & String5 & "' " _
            & "WHERE ID = " & ListBox1.Items(ListBox1.SelectedIndex)


            MyOledbCommand.Connection = OleDbConn

            MyOledbCommand.ExecuteNonQuery()
            OleDbConn.Close()


            DisableTextboxes()
            ProgressBar1.Value = 0

            FillDataGrid("Select * from Table1")
            FillListBox("Select * from Table1")

            cmdNew.Enabled = True
            cmdDelete.Enabled = True

            SaveOrEdit = "Cancel"

            StatusBar1.Text = " 数据已修改."
        Catch err As System.Exception
            StatusBar1.Text = err.Message
        End Try

    End Function



    Public Function Delete() As String
        Try

            Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnString)
            OleDbConn.Open()


            Dim MyOledbCommand As OleDbCommand = New OleDbCommand()


            MyOledbCommand.CommandText = "Delete From Table1 " _
                                        & "WHERE ID = " & ListBox1.Items(ListBox1.SelectedIndex)

            MyOledbCommand.Connection = OleDbConn

            MyOledbCommand.ExecuteNonQuery()
            OleDbConn.Close()

            DisableTextboxes()
            ProgressBar1.Value = 0

            FillDataGrid("Select * from Table1")
            FillListBox("Select * from Table1")
            StatusBar1.Text = " 数据已删除."


        Catch err As System.Exception
            StatusBar1.Text = err.Message
        End Try

    End Function



    Public Function DeleteAll() As String
        Try

            Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnString)
            OleDbConn.Open()


            Dim MyOledbCommand As OleDbCommand = New OleDbCommand()


            MyOledbCommand.CommandText = "Delete * From Table1"

            MyOledbCommand.Connection = OleDbConn

            MyOledbCommand.ExecuteNonQuery()
            OleDbConn.Close()

            DisableTextboxes()
            ProgressBar1.Value = 0

            FillDataGrid("Select * from Table1")
            FillListBox("Select * from Table1")
            StatusBar1.Text = " 数据已删除."


        Catch err As System.Exception
            StatusBar1.Text = err.Message
        End Try

    End Function



    Public Function EnableTextboxes(ByVal ClearTextBoxes As Boolean)



        TextBox1.ReadOnly = False
        TextBox2.ReadOnly = False
        TextBox3.ReadOnly = False
        TextBox4.ReadOnly = False
        TextBox5.ReadOnly = False


        If ClearTextBoxes = True Then
            TxtId.Text = vbNullString
            TextBox1.Text = vbNullString
            TextBox2.Text = vbNullString
            TextBox3.Text = vbNullString
            TextBox4.Text = vbNullString
            TextBox5.Text = vbNullString
        End If


    End Function



    Public Function DisableTextboxes()

        TxtId.ReadOnly = True

        TextBox1.ReadOnly = True
        TextBox2.ReadOnly = True
        TextBox3.ReadOnly = True
        TextBox4.ReadOnly = True
        TextBox5.ReadOnly = True

        TxtId.Text = vbNullString
        TextBox1.Text = vbNullString
        TextBox2.Text = vbNullString
        TextBox3.Text = vbNullString
        TextBox4.Text = vbNullString
        TextBox5.Text = vbNullString


    End Function


#End Region



#Region " Objektet dhe kodi "



    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim SqlStr As String

        SqlStr = "Select * from Table1 where ID = " & ListBox1.Items(ListBox1.SelectedIndex)

        FillTextBox(SqlStr)

    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DisableTextboxes()
        FillListBox("Select * from Table1")
        FillDataGrid("Select * from Table1")
    End Sub



    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        DisableTextboxes()
        ProgressBar1.Value = 0
        FillDataGrid("Select * from Table1")
        FillListBox("Select * from Table1")

    End Sub



    Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click

        SaveOrEdit = "Save"
        StatusBar1.Text = " 添加新数据"
        EnableTextboxes(True)

        cmdEdit.Enabled = False
        cmdDelete.Enabled = False

    End Sub



    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try

            Dim CatchError As Integer = ListBox1.Items(ListBox1.SelectedIndex)

        Catch err As System.Exception

            StatusBar1.Text = " 修改所选择记录"
            Exit Sub
        End Try



        SaveOrEdit = "Edit"
        StatusBar1.Text = " 修改数据"

        EnableTextboxes(False)

        cmdNew.Enabled = False
        cmdDelete.Enabled = False


    End Sub



    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click

        Try

            Dim CatchError As Integer = ListBox1.Items(ListBox1.SelectedIndex)

        Catch err As System.Exception

            StatusBar1.Text = " 删除所选择数据"
            Exit Sub
        End Try




        Delete()

    End Sub



    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

        If SaveOrEdit = "Save" Then

            AddNew()

        ElseIf SaveOrEdit = "Edit" Then

            Edit()
        Else

            StatusBar1.Text = "没有数据需要保存"

        End If

    End Sub



    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click

        Try

            ListBox1.ClearSelected()

        Catch err As System.Exception


        End Try

        'ketu ja japim vleres saveOredit ne Cancel dhe ne rast se shtypim save apo edit 
        'pa selektuar ndonjen record do te marrim mesazhin "Zgjidh nje record"
        SaveOrEdit = "Cancel"
        DisableTextboxes()
        StatusBar1.Text = " 取消"


        cmdNew.Enabled = True
        cmdDelete.Enabled = True
        cmdEdit.Enabled = True

    End Sub


    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        'vendosi te dhenat ne DataGrid
        FillDataGrid("Select * from Table1 where Column1 = " & "'" & txtSearch.Text & "'")
        ProgressBar1.Value = 0
        'vendosi te dhenat ne ListBox
        FillListBox("Select * from Table1 where Column1 = " & "'" & txtSearch.Text & "'")

    End Sub




#End Region



    

    
    Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
       
        Dim SqlStr As String

        SqlStr = "Select * from Table1 where ID = " & DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)

        FillTextBox(SqlStr)
    End Sub

    

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -