我找到了一个很好的解决方案,它涉及几个设置步骤,但没有
难的。请记住,服务器和数据库需要相同
在架构中,但我有一轮工作,请参阅第 5 步。
1) 创建您的 DBML 文件并将一些表拖到其中。我的被称为“数据库”。
您现在必须对 DBML 的数据上下文属性做一些事情,另外:
A)展开连接属性并清除您在那里看到的任何连接字符串。
B) 将应用程序设置设置为 false,因此项目不使用任何设置。
C) 删除 DBML 自动从 DBML 创建的任何连接字符串(设置为无)
(我这样做是为了确保设置文件连接没有覆盖我的运行时代码)
2) 像这样将静态 SQL 服务器连接添加到 app.config 文件。
<pre>
<appSettings>
<add key="BGConnectionString" value="bg_prodConnectionString"/>
<add key="DelphosConnectionString" value="delphos_prodConnectionString"/>
<add key="TiffinConnectionString" value="tiffin_prodConnectionString"/>
</appSettings>
<connectionStrings>
<add name="bg_prodConnectionString" connectionString="Data Source=tmd-bg-sql;Initial Catalog=bg_prod;Persist Security Info=True;User ID=sa"
providerName="System.Data.SqlClient" />
<add name="delphos_prodConnectionString" connectionString="Data Source=tmd-d2-sql;Initial Catalog=delphos2_prod;Persist Security Info=True;User ID=sa"
providerName="System.Data.SqlClient" />
<add name="tiffin_prodConnectionString" connectionString="Data Source=tmd-tiffin-sql;Initial Catalog=tiffin_prod;Persist Security Info=True;User ID=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
<code>
3)这里的想法是在所有表单中使用一个且只有一个数据上下文,并且
应用程序的类等,因此设置一个公共变量。我们想做的是
给它一个初始连接字符串,以后可以更改。
<pre>
Public dc As New DataBaseDataContext("Data Source=tmd-bg-sql;Initial Catalog=bg_prod;Persist Security Info=True;User ID=sa")
<code>
4) 使用某种变量来确定您的服务器/数据库“覆盖”如何工作。
我在 MyProject (VB) 中使用了一个名为“location”设置的 My.settings 字符串变量。这是
保存在启动时调用的另一种形式。
现在“覆盖”一次(程序启动表单或模块),并允许它在全球范围内使用。
<pre>
' save this first settings first before executing this code block
Select Case My.Settings.Location
Case "Bowling Green"
' Read the ConnectionStrings from the config file
Dim ConnectionString = ConfigurationManager.ConnectionStrings(ConfigurationManager.AppSettings.Get("BGConnectionString")).ConnectionString
dc.Connection.ConnectionString = ConnectionString
Try
Dim query = From emp In dc.employees _
Select emp.name, emp.employee_id _
Order By name
For Each emp In query
MsgBox("BG")
Exit For
'MsgBox(emp.name)
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Case "Delphos"
' Read the ConnectionStrings from the config file
Dim ConnectionString = ConfigurationManager.ConnectionStrings(ConfigurationManager.AppSettings.Get("DelphosConnectionString")).ConnectionString
dc.Connection.ConnectionString = ConnectionString
Dim query = From emp In dc.employees _
Select emp.name, emp.employee_id _
Order By name
For Each emp In query
MsgBox("Delphos")
Exit For
Next
Case "Tiffin"
' Read the ConnectionStrings from the config file
Dim ConnectionString = ConfigurationManager.ConnectionStrings(ConfigurationManager.AppSettings.Get("TiffinConnectionString")).ConnectionString
dc.Connection.ConnectionString = ConnectionString
Dim query = From emp In dc.employees _
Select emp.name, emp.employee_id _
Order By name
For Each emp In query
MsgBox("Tiffin")
Exit For
Next
End Select
' reference the dc context connection from another form or class
' instance the MDI form to get the public connection
Dim MDI As New MDIParent
Dim query = From emp In MDI.dc.employees _
Select emp.name, emp.employee_id _
Order By name
For Each emp In query
MsgBox("Tiffin")
Exit For
Next
<code>
5) 我现在可以使用 3 个不同的数据库连接和访问 3 个不同的 SQL 服务器
使用最少的代码。现在,我的数据库几乎是相同的,但如果不是,这里有一个技巧。
我的员工表中碰巧有一个额外的字段列。我把那个拖进了
数据库ML。只要您在 LINQ 查询中指定您希望避免的列名
当查询“覆盖”到另一个数据库时出现异常错误。
我不能为此承担所有功劳。
我从这里找到了其他代码。
Click for Code
这里:
And click here