从Modified Datatable更新SQL数据库

时间:2012-02-24 02:25:03

标签: datagridview datatable

我有一个应用程序,我使用以下函数填充多个DataGridviews。

dgvMyStats.DataSource = GetDataTable("Select * from MyMonthlySalesStats")

Protected Friend Function GetDataTable(ByVal sqlCommand As String) As DataTable

    Dim connectionString As String = ConfigurationManager.ConnectionStrings("iView.My.MySettings.iViewConnectionString").ConnectionString
    Dim iViewConnection As SqlConnection = New SqlConnection(connectionString)
    Dim command As New SqlCommand(sqlCommand, iViewConnection)
    Dim adapter As SqlDataAdapter = New SqlDataAdapter()
    Dim table As New DataTable

    adapter.SelectCommand = command

    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    adapter.Fill(table)

    ' Close Connection
    iViewConnection.Close()

    Return table

End Function

这就像一个魅力。但是,现在我需要从我的一个datagridviews更新我的数据库。我已经更改了datagridview的属性以允许创建新行。但现在我需要知道如何将这个更新的datagridview数据推送回我的SQL数据库。任何帮助都很有用。提前谢谢......

1 个答案:

答案 0 :(得分:0)

我明白了。我不得不建立另一个功能

Protected Friend Function SQLDirect(ByVal sqlCommand As String)

    Dim connectionString As String = ConfigurationManager.ConnectionStrings("iView.My.MySettings.iViewConnectionString").ConnectionString
    Dim iViewConnection As SqlConnection = New SqlConnection(connectionString)
    Dim command As New SqlCommand(sqlCommand, iViewConnection)
    Dim returnValue As String

    ' Open Connection
    iViewConnection.Open()

    ' Execute SQL Command
    returnValue = Convert.ToInt32(command.ExecuteScalar())

    ' Close Connection
    iViewConnection.Close()

    ' Return Value back to calling procedure
    Return returnValue

End Function

我使用此功能提交我的更新和插入。我希望这有助于其他人。感谢...