我有点奇怪,我一直在网上看,但找不到任何特定的问题。希望有人可以帮助我。
我有一个从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”
所以我想创建一个新的Function,将所有更改保存回SQL数据库。当我使用相同的全局变量来填充具有可以同时存在的多个实例的单个表单时,我需要弄清楚如何从DataGridView传递信息或者沿着那些行传递信息而我被卡住了。
非常感谢任何帮助。
答案 0 :(得分:0)
我最终找到了一个解决方案,并且认为我会发布它,因为其他任何人都会遇到同样的问题。
我最终确定只使用以下代码打开一个可编辑窗口:
Public sqlEditForm As SQLeditor
If issql = True Then
If sqlEditForm Is Nothing OrElse sqlEditForm.IsDisposed Then
sqlEditForm = New SQLeditor
sqlEditForm.MdiParent = Main
sqlEditForm.Show()
Else
Dim reply As DialogResult
reply = MsgBox("An existing edit window is already active, do you want to close the existing instance and create a new one?" & vbCrLf & "Any unsaved changes will be lost?", MsgBoxStyle.YesNoCancel, "Existing Window Detected")
Select Case reply
Case Is = vbYes
sqlEditForm.Close()
sqlEditForm = New SQLeditor
sqlEditForm.MdiParent = Main
sqlEditForm.Show()
Case Is = vbNo
sqlEditForm.Show()
Case Is = vbCancel
End Select
End If
然后只需从数据适配器保存一个数据实例。
Public Sub save_SQL()
Try
Dim myAdapter As New MySqlDataAdapter
Dim MysqlConn As New MySqlConnection
Dim cmdb = New MySqlCommandBuilder(myAdapter)
MysqlConn.ConnectionString = "server=" & sqlServer & ";" _
& "user id=" & sqlUsername & ";" _
& "password=" & sqlPassword & ";" _
& "database=" & sqlDatabase
MysqlConn.Open()
Select Case sqlclick
Case Is = "Office Details"
myAdapter.SelectCommand = New MySqlCommand(qryOffice, MysqlConn)
Case Is = "School Details"
myAdapter.SelectCommand = New MySqlCommand(qrySchools, MysqlConn)
Case Is = "ICT Staff"
myAdapter.SelectCommand = New MySqlCommand(qryITstaff, MysqlConn)
Case Is = "SQL Editor"
myAdapter.SelectCommand = New MySqlCommand(qrycustom, MysqlConn)
Case Else
End Select
myAdapter.Update(SQLdata)
MysqlConn.Close()
MysqlConn.Dispose()
Catch ex As Exception
Main.lblStatus.Text = "SQL Connection Error"
errMsg = ex.ToString
errForm = "SQL Connection"
errWrite()
End Try
End Sub
将同一系统调整为多MDI模型应该不难,但我认为我不需要它。