0

这要交给天才,让我们看看你能不能帮助我。

Imports System
Imports System.Data

' Version 6.5.4.0 Runtime v2.0.50727
Imports MySql.Data.MySqlClient

Public Class MainForm

  ' Config - Main. 
  Private db_host As String = ""
  Private db_username As String = ""
  Private db_userpass As String = ""
  Private db_catalog As String = ""
  Private db_port As String = "3306"

  ' Config - Specific Table.
  Private db_specific_table As String = "SF6SETUP"

  ' Config - Other.
  Private DS As DataSet
  Private DA As MySqlDataAdapter
  Private BS As BindingSource

  ' ***
  '
  ' ***
  Private Sub My_Init()

    Dim conn As String = "Data Source = " & db_host & ";Initial Catalog=" & db_catalog & "; uid=" & db_username & ";password=" & db_userpass
    Dim myConnection As New MySqlConnection(conn)
    Dim cmd As String = "SELECT * FROM " & db_specific_table

    DA = New MySqlDataAdapter(cmd, myConnection)
    ' This line of code to generate update commands automatically.
    ' This update method of would not work without this line of code. 
    Dim MySQLCommandBuilder As New MySqlCommandBuilder(DA)

    myConnection.Open()

    DS = New DataSet()
    DA.Fill(DS, "MyTable")

    BS = New BindingSource
    BS.DataSource = DS.Tables(0)

    Dim BN As BindingNavigator = New BindingNavigator()
    BN.BindingSource = BS
    DataGridView.DataSource = BS

  End Sub

  ' ***
  '
  ' ***
  Private Sub MainForm_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    My_Init()

  End Sub

  Private Sub DataGridView_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView.KeyPress
    BS.EndEdit()
    DA.Update(DS, "MyTable")
  End Sub
End Class

有一个错误:

System.Data.dll 中出现“System.InvalidOperationException”类型的未处理异常

附加信息:对于不返回任何键列信息的 SelectCommand,不支持为 UpdateCommand 生成动态 SQL。

这段代码工作除了没有保存到 MySQLDatabase 的更改!!!请帮我解决这个问题。

谢谢,

麦克风

4

1 回答 1

1

根据错误,您的表没有主键 - 您如何期望 update 知道要更新哪一行(可能有重复)。

于 2012-06-05T23:22:23.517 回答