0

我创建了一个连接到数据库的程序

问题就像我将名称“San Fransio”更改为“Steve”当我转到下一页并返回我刚刚编辑的页面时,它没有更新,名称仍然显示“San Fransio”但是当我关闭时它并重新运行程序,它只更新为“史蒂夫”。
有什么方法可以在不关闭数据的情况下更新和刷新数据?
我需要添加一些刷新数据代码或什么?

Public Class Form2  
    Dim cnn As New OleDb.OleDbConnection  
    Dim sql As String = "SELECT * FROM sr"  
    Dim ds As New DataSet  
    Dim da As OleDb.OleDbDataAdapter  
    Dim i As Integer  
    Dim len As Integer  
    Dim ind As Integer = 0  
    Dim arrNumbers() As String  
    Dim no As Integer  



    Private Sub btnSrch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click
        Dim sql1 = InputBox("Service Number :", "Search", " ")
        Dim no As Integer
        Dim found As Boolean = False

        For no = 0 To len - 1
            If ds.Tables("db").Rows(no).Item(0).ToString.ToLower = sql1.ToString.ToLower Then
                i = (no)
                nav()
                found = True

            End If

        Next
        If (found = False) Then
            MsgBox("Search Not Found")
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cnn = New OleDb.OleDbConnection
        cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=C:\Users\Lim\Documents\db.mdb"
        da = New OleDb.OleDbDataAdapter(sql, cnn)
        da.Fill(ds, "db")
        len = da.Fill(ds, "db")

        i = len - 1
        nav()

    End Sub
    Private Sub nav()
        txtsr.Text = ds.Tables("db").Rows(i).Item(0)
        txtname.Text = ds.Tables("db").Rows(i).Item(1)
        txttel.Text = ds.Tables("db").Rows(i).Item(2)
        txtdate.Text = ds.Tables("db").Rows(i).Item(3)
        txtprob.Text = ds.Tables("db").Rows(i).Item(4)
        txtmodel.Text = ds.Tables("db").Rows(i).Item(5)
        txtacc.Text = ds.Tables("db").Rows(i).Item(6)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (i = 0) Then
            MsgBox("This is the first page")
        Else
            i = i - 1
            nav()

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If (i = len - 1) Then
            MsgBox("This is the last page")
        Else
            i = i + 1
            nav()
        End If
    End Sub



    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        Dim cnn As New OleDb.OleDbConnection
        Dim cmd As New OleDb.OleDbCommand
        cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=C:\Users\Lim\Documents\db.mdb"
        cnn.Open()

        Dim sqlstr = "UPDATE sr set cname='" & txtname.Text & "',tel='" & txttel.Text & "',cdate='" & txtdate.Text & "',prob='" & txtprob.Text & "',model='" & txtacc.Text & "'WHERE SR=" & txtsr.Text & ""

        cmd = New OleDb.OleDbCommand(sqlstr, cnn)
        cmd.ExecuteNonQuery()

        cnn.Close()


    End Sub

End Class
4

1 回答 1

1

上面的代码有很多问题。

  • 打开连接时不会关闭连接
  • 您不为更新查询使用参数
  • 您不使用 OleDbDataAdapter.UpdateCommand 更新您的表

关闭连接

    Using cnn = New OleDb.OleDbConnection
       .....
    End Using

使用参数

 Dim sqlstr = "UPDATE sr set cname=?,tel=?,cdate=?,prob=?,model=? WHERE SR=?"
 da.UpdateCommand = new OleDbCommand(sqlstr, cnn)
 da.UpdateCommand.AddWithValue("@cName", txtname.Text)
 da.UpdateCommand.AddWithValue("@tel", txttel.Text)

 ' and so on but remember to follow exactly the order in which they are expected
于 2012-11-08T10:56:58.797 回答