vb.net 从 DataGridView 更新 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14831144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:17:49  来源:igfitidea点击:

Update SQL From DataGridView

sqlvb.netsql-server-2008visual-studio-2008datagridview

提问by user1295053

I am populating a DataGridView from a SQL Server 2008 database table. I then allow users to edit the values in the DataGridView, what I am struggling on is how to then write these changes back to the SQL table on a button press?

我正在从 SQL Server 2008 数据库表中填充 DataGridView。然后我允许用户编辑 DataGridView 中的值,我正在努力的是如何在按下按钮时将这些更改写回 SQL 表?

I am populating using this:

我正在使用这个填充:

 Try

            Dim command As New SqlCommand
            Dim adapter As New SqlDataAdapter

            DutyRef = DutyRef.Trim
            Dim DutyDetails As String = "SELECT * from MyTable WHERE Col1 = 1"


            If ds1.Tables("DT_Test").Rows.Count > 0 Then
                ds1.Tables("DT_Test").Rows.Clear()
                Tab2_DGVDuty.Refresh()
            End If

            SQLConn = New SqlConnection(SQLConnString)

            command = New SqlCommand(DutyDetails, SQLConn)
            adapter.SelectCommand = command
            adapter.Fill(ds1, "DT_Test")

            Tab2_DGVDuty.DataSource = ds1.Tables("DT_Test")
            Tab2_DGVDuty.Columns("ID").Visible = False


            adapter.Dispose()
            command.Dispose()
            SQLConn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try