更新后刷新DataGridView,删除插入新记录VB .Net

时间:2018-08-25 23:03:42

标签: database vb.net datagridview access

我正在尝试构建一个程序以在Access数据库中插入新记录, 我还想查看活着的记录,并在每次单击添加按钮之一后保持刷新。你应该知道 : -我使用的是单个form1。 -我正在使用Access DB(.mdb)。 -我正在使用Visual Studio 2017。 -我正在使用Microsoft Access 2016。 我也是VB .net的新手。

最后这是我的代码:

Imports System.Data.OleDb
Module modConnection

Public cn As New OleDb.OleDbConnection
Public cm As New OleDb.OleDbCommand
Public dr As OleDbDataReader

Public Sub connection()
    cn = New OleDb.OleDbConnection
    With cn
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
& Application.StartupPath & "\sample.mdb"
        .Open()
    End With
End Sub
End Module

Public Class Form1
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource



Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles btnFind.Click
    Dim found As Boolean
    Try

        cm = New OleDb.OleDbCommand
        With cm
            .Connection = cn
            .CommandType = CommandType.Text
            .CommandText = "SELECT * FROM tblStudent WHERE (Snum = '" & 
txtSearch.Text & "')"
            dr = .ExecuteReader
        End With
        While dr.Read()

            txtName.Text = dr("Sname").ToString
            txtCourse.Text = dr("Scourse").ToString
            txtSection.Text = dr("Ssection").ToString
            found = True

        End While
        cn.Close()
        Exit Sub
        If found = False Then MsgBox("Student ID not found.", 
MsgBoxStyle.Critical)
        dr.Close()
    Catch ex As Exception

    End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'SampleDataSet.tblStudent' 
table. You can move, or remove it, as needed.
    Me.TblStudentTableAdapter.Fill(Me.SampleDataSet.tblStudent)
    Call connection()
End Sub

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

        cm = New OleDb.OleDbCommand
        With cm
            .Connection = cn
            .CommandType = CommandType.Text
            .CommandText = "INSERT INTO tblStudent 
(Snum,Sname,Scourse,Ssection) VALUES (@Snum,@Sname,@Scourse,@Ssection)"

            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Snum", 
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtSearch.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Sname", 
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtName.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Scourse", 
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtCourse.Text))
            .Parameters.Add(New 
System.Data.OleDb.OleDbParameter("@Ssection", 
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtSection.Text))


            ' RUN THE COMMAND
            cm.Parameters("@Snum").Value = Me.txtSearch.Text
            cm.Parameters("@Sname").Value = Me.txtName.Text
            cm.Parameters("@Scourse").Value = Me.txtCourse.Text
            cm.Parameters("@Ssection").Value = Me.txtSection.Text


            cm.ExecuteNonQuery()
            MsgBox("Record saved.", MsgBoxStyle.Information)
            Me.txtCourse.Text = ""
            Me.txtName.Text = ""
            Me.txtSearch.Text = ""
            Me.txtSection.Text = ""
            Exit Sub
        End With
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
    Me.TblStudentTableAdapter.Fill(Me.SampleDataSet.tblStudent)
    Call connection()

    ShowData.Items.Refresh();

End Sub

End Class

我真的需要帮助, 谢谢先进的家伙。

0 个答案:

没有答案