我有点奇怪,我一直在网上寻找,但找不到任何特定于我的问题的东西。希望有人可以帮助我。
我有一个从 MySQL 读取信息的 VB.Net 2012 应用程序。读数很好,并且填充正确,但我无法弄清楚如何保存更改。
信息从 DataTable 显示到 DataGridView。该应用程序是一个 MDI 应用程序,在任何时候都可能有多个 TableEditor 表单作为子项打开。
这是我的信息加载功能:
Public Function get_SQL(ByVal SQLquery As String) As DataTable
Try
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim MysqlConn As New MySqlConnection
MysqlConn.ConnectionString = "server=" & sqlServer & ";" _
& "user id=" & sqlUsername & ";" _
& "password=" & sqlPassword & ";" _
& "database=" & sqlDatabase
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQLquery
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
MysqlConn.Close()
MysqlConn.Dispose()
Return (myData)
Catch ex As Exception
Main.lblStatus.Text = "SQL Connection Error"
errMsg = ex.ToString
errForm = "SQL Connection"
errWrite()
End Try
End Function
并且信息在加载时加载到子表单中,如下所示:
Private Sub SQLeditor_Load(sender As Object, e As EventArgs) Handles Me.Load
ToolStrip1.Visible = False
Select Case sqlclick
Case Is = "Office Details"
SQLdata = get_SQL(qryOffice)
Case Is = "School Details"
SQLdata = get_SQL(qrySchools)
Case Is = "ICT Staff"
SQLdata = get_SQL(qryITstaff)
Case Else
ToolStrip1.Visible = True
End Select
dgvEditor.DataSource = SQLdata
End Sub
SQLdata 是一个全局变量“Public SQLdata As DataTable”
所以我想创建一个新函数,它将所有更改保存回 SQL 数据库。由于我使用相同的全局变量来填充具有多个可以同时存在的实例的单个表单,因此我需要弄清楚如何从 DataGridView 传递信息或类似的东西,但我被卡住了。
任何帮助深表感谢。