我正在使用我的sql server中的存储过程来创建datagridview。
当我更新我的存储过程时,例如添加列,我的datagridview不会改变。
如何刷新sqladapater,然后我的datagridview会添加列?
答案 0 :(得分: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