c datagridview mysql_使用DataGridView进行MySQL更新

VB.NET应用程序尝试从数据库加载数据并自动创建更新命令时遇到问题。错误表明`SelectCommand`未返回任何键列信息,导致无法动态生成SQL更新语句。为解决此问题,你需要确保查询返回包含主键列的信息,以便数据适配器能够正确生成更新命令。

这到了那里的天才,让我们看看你能帮助我。

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生成。

此代码的作品除了没有更改保存到MySQL数据库!

请帮我解决这个问题。

谢谢,

麦克风

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值