【发布时间】:2016-02-11 16:04:52
【问题描述】:
这个问题以前可能有人问过和回答过,但我不知道如何搜索。
情况:
我有一个带有许多绑定控件的Winforms 应用程序。我们有四个测试环境,目前,我一直在连接到每个数据源并使用Data Source Configuration Manager 导入所有数据表和存储过程,然后将连接字符串信息存储在我的 app.config 文件中。当我调用连接字符串时,我使用的是 My.Settings.[连接字符串名称].ToString(我的第一个问题)。
对于所有数据绑定控件(主要是datagridview),我在拖拽新数据集+表适配器和绑定源之前,先去掉旧数据集+表适配器和所有绑定源。我有一个 Public Sub 可以清除并重新绑定我的所有控件,但代码需要每个数据源的特定名称(例如,请参见下文)。
Public Sub SetRegionDEBindings(ByVal errorCode As String, ByVal Region As String)
Try
Dim selectLOMAbyRegion As New BindingSource
selectLOMAbyRegion = Me.SpSelectLOMABindingSource
Me.SpSelectLOMATableAdapter.Fill(Me.PrdGISDataSet.spSelectLOMA, errorCode, Region)
Me.dgDE.DataSource = selectLOMAbyRegion
Me.bnDE.BindingSource = selectLOMAbyRegion
Me.txtDEIssueDte.DataBindings.Add(New System.Windows.Forms.Binding("Text", selectLOMAbyRegion, "IssueDte", True))
Me.txtDECaseNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", selectLOMAbyRegion, "CaseNum", True))
Me.txtDECommNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", selectLOMAbyRegion, "CommNum", True))
With Me.dgDE
Me.DataGridViewTextBoxColumn1.Name = "DERegion"
Me.StateDataGridViewTextBoxColumn.Name = "DEState"
Me.LatitudeDataGridViewTextBoxColumn.Name = "Latitude"
Me.LongitudeDataGridViewTextBoxColumn.Name = "Longitude"
Me.IdLOMADataGridViewTextBoxColumn.Visible = False
Me.DiskNumDataGridViewTextBoxColumn.Visible = False
Me.DataGridViewTextBoxColumn1.Visible = False
Me.InsertDteDataGridViewTextBoxColumn.Visible = False
Me.LOMAStatusDataGridViewTextBoxColumn.Visible = False
Me.NotesDataGridViewTextBoxColumn.Visible = False
Me.StateDataGridViewTextBoxColumn.Visible = False
Me.SupercedeDataGridViewCheckBoxColumn.Visible = False
Me.UpdatedDteDataGridViewTextBoxColumn.Visible = False
End With
Me.dgDE.Visible = True
Me.dgDE.Rows(0).Selected = True
Me.dgDE.BringToFront()
If Me.dgDE.Rows(0).Cells("LOMAStatusDataGridViewTextBoxColumn").Value.ToString = "2" Then
MessageBox.Show("Warning: this case number has been previously inserted.")
End If
Catch ex As Exception
MessageBox.Show("Error setting data entry bindings: " & ex.Message.ToString)
End Try
End Sub
我的问题:
我应该从头开始创建 app.config 文件吗?
如果我需要更改服务器环境,是否可以只更改 app.config 文件(并调用
Configuration.ConfigurationManager而不是My.Settings...)?如果我更改 app.config 文件,如何在不打开解决方案、重新创建数据源和清理/重新构建的情况下重新绑定我的数据源? (因为目前我连接新的
data source,将绑定源拖到我的控件上,手动更新数据集代码)
我看过这里:
How To Change The Connection String saved in My.Settings in VB 2010
https://msdn.microsoft.com/en-us/library/ms171889(v=vs.90).aspx
但是,就像我说的,我不确定我是否正在寻找正确的答案。
【问题讨论】:
-
到底是什么问题,你想做什么?
-
我需要能够更改 app.config 文件中的连接字符串,并让应用程序中的数据源自动重新绑定到该连接字符串(无需进入解决方案,将正确的数据源元素到我的控件和清理/重建)
标签: vb.net winforms data-binding connection-string