0

我正在使用我的 sql 服务器中的存储过程来创建一个 datagridview。

当我更新存储过程(例如添加列)时,我的 datagridview 不会更改。

如何刷新我的 sqladapater 然后我的 datagridview 将添加该列?

4

1 回答 1

0
 Private Sub ClickButton()


        Dim dt As New System.Data.DataTable
        Dim con As New System.Data.SqlClient.SqlConnection("connectionString")
        Dim strSQL As String = "EXEC sp_myprocedure"
        Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, con)

        da.Fill(dt)

        mydatagridview.DataSource = dt


End Sub

这里有一些适当的代码来完成实际工作:

Public Shared Function GetDataTable(cmd As System.Data.IDbCommand) As System.Data.DataTable
    Return GetDataTable(cmd, Nothing)
End Function ' End Function GetDataTable


Public Shared Function GetDataTable(cmd As System.Data.IDbCommand, strTableName As String) As System.Data.DataTable
    Dim dt As New System.Data.DataTable

    If Not String.IsNullOrEmpty(strTableName) Then
        dt.TableName = strTableName
    End If

    Using idbc As System.Data.IDbConnection = GetConnection()

        SyncLock idbc

            SyncLock cmd

                Try
                    cmd.Connection = idbc

                    Using daQueryTable As System.Data.Common.DbDataAdapter = m_ProviderFactory.CreateDataAdapter()

                        SyncLock daQueryTable
                            daQueryTable.SelectCommand = DirectCast(cmd, System.Data.Common.DbCommand)
                            daQueryTable.Fill(dt)
                        End SyncLock

                    End Using ' daQueryTable

                    ' End Try
                Catch ex As Exception
                    'COR.Debug.MsgBox("Exception executing ExecuteInTransaction: " + ex.Message);
                    Log(ex, String.Format("Exception executing GetDataTable (cmd As System.Data.IDbCommand): {0}", ex.Message.ToString() + vbCrLf + "strSQL=" + GetParametrizedQueryText(cmd)))

                    Return Nothing
                Finally
                    ' End Catch
                    If idbc.State <> System.Data.ConnectionState.Closed Then
                        idbc.Close()
                    End If

                End Try

            End SyncLock ' cmd

        End SyncLock ' idbc

    End Using 'idbc

    'Dim iResultCOunt As Integer = dt.Rows.Count

    Return dt
End Function ' GetDataTable
于 2013-05-23T12:02:22.987 回答