0

我有点奇怪,我一直在网上寻找,但找不到任何特定于我的问题的东西。希望有人可以帮助我。

我有一个从 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 传递信息或类似的东西,但我被卡住了。

任何帮助深表感谢。

4

1 回答 1

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 模型应该不难,但我认为我不需要它。

于 2013-02-06T13:41:03.493 回答