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' の未処理の例外が発生しました

追加情報: UpdateCommand の動的 SQL 生成は、キー列情報を返さない SelectCommand に対してはサポートされていません。

このコードは、変更が MySQLDatabase に保存されていない場合を除いて機能します!!! これを解決するのを手伝ってください。

ありがとうございました、

マイク

4

1 に答える 1

1

エラーによると、テーブルには主キーがありません - update がどの行を更新するかをどのように知ることができますか (重複がある可能性があります)。

于 2012-06-05T23:22:23.517 に答える