0

**我有简单的应用程序,但我不知道如何修复它。当我尝试编辑我的数据库时这张照片-> http://i861.photobucket.com/albums/ab171/gopak/sa_zps5a950df5.jpg

当我单击按钮编辑时,我希望我的访问数据将被更新。这是我的代码..感谢您的建议**

Imports System.Data.OleDb
Public Class Form2
Public cnstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gop's\Downloads\admin site\admin site\admin site\bin\Debug\data_ruangan.accdb"""
Public cn As New OleDbConnection
Public cmd As New OleDbCommand
Public adaptor As New OleDbDataAdapter

Private Sub logout_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles logout_btn.Click
    Form1.Show()
    Me.Close()

End Sub

Private Sub exit_btn_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exit_btn.Click
    Dim a As Integer
    a = MsgBox("Are you sure want to exit application?", vbInformation + vbYesNo, "Admin Site Virtual Tour Application")
    If a = vbYes Then
        End
    Else
        Me.Show()
    End If
End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'Data_ruanganDataSet.data_ruangan' table. You can move, or remove it, as needed.
    Me.Data_ruanganTableAdapter.Fill(Me.Data_ruanganDataSet.data_ruangan)

End Sub

Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim i = DataGridView1.CurrentRow.Index
    Label7.Text = DataGridView1.Item(0, i).Value
    txtName.Text = DataGridView1.Item(1, i).Value
    txtLocation.Text = DataGridView1.Item(2, i).Value
    txtCapacity.Text = (DataGridView1.Item(3, i).Value).ToString
    txtOperational.Text = (DataGridView1.Item(4, i).Value).ToString
    txtInformation.Text = DataGridView1.Item(5, i).Value
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'If txtName.Text <> "" And txtLocation.Text <> "" And txtCapacity.Text <> "" And txtOperational.Text <> "" And txtInformation.Text <> "" Then
    Dim i = DataGridView1.CurrentRow.Index
    Dim ID = DataGridView1.Item(0, i).Value
    Dim cmd As New OleDb.OleDbCommand

    If Not cn.State = ConnectionState.Open Then
        cn.Open()
    End If

    cmd.Connection = cn
    cmd.CommandText = ("update data_ruangan set Name = '" & txtName.Text & _
        "',Location = '" & txtLocation.Text & "',Capacity = '" & txtCapacity.Text & _
        "',Operational_Hours ='" & txtOperational.Text & "',Information = '" & txtInformation.Text & ";")
    cmd.ExecuteNonQuery()
    cn.Close()
    txtName.Text = ""
    txtLocation.Text = ""
    txtCapacity.Text = ""
    txtOperational.Text = ""
    txtInformation.Text = ""
    'End If
End Sub

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

  End Sub
End Class
4

1 回答 1

0

尝试使用参数化查询来执行您的代码。该错误消息与您尚未使用 ConnectionString 中包含的信息初始化连接这一事实有关。
这是一个关于如何做到这一点的例子......但是......

Dim cmdText = "update data_ruangan set [Name] = ?,Location = ?,Capacity = ?, " & _
              "Operational_Hours =?,Information = ?;"
Using cn = new OleDbConnection( cnstring )
Using cmd = OleDb.OleDbCommand(cmdText, cn)
    cn.Open
    cmd.Parameters.AddWithValue("@p1", txtName.Text)
    cmd.Parameters.AddWithValue("@p2", txtLocation.Text)
    cmd.Parameters.AddWithValue("@p3", txtCapacity.Text)
    cmd.Parameters.AddWithValue("@p4", txtOperational.Text)
    cmd.Parameters.AddWithValue("@p5", txtInformation.Text)

    '''' WARNING ''''
    ' WITHOUT A WHERE STATEMENT YOUR QUERY WILL UPDATE 
    ' THE WHOLE TABLE WITH THE SAME VALUES
    '''' WARNING ''''

    cmd.ExecuteNonQuery()
End Using
End Using
txtName.Text = ""
txtLocation.Text = ""
txtCapacity.Text = ""
txtOperational.Text = ""
txtInformation.Text = ""

此代码使用相同的值更新表的所有记录,因此,除非您只有一条记录并更新始终相同的记录,否则您需要在命令中添加 WHERE 条件。

此外,NAME 字是 Access 2007/2010 中的保留关键字,最好用方括号封装该字以避免出现语法错误消息。

我还删除了全局变量 OleDbConnection 并使用了一个本地变量,当代码从 Using 语句中退出时,该变量将被关闭和销毁。这是处理一次性对象的正确方法,特别是每个连接对象总是要以这种方式使用,以尽快释放对象使用的昂贵的非托管资源。

于 2013-06-16T13:44:02.640 回答