SQL 如何通过一个连接字符串Sql和Vb.Net连接两个不同的数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13687303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect with two different databases by one connection string Sql and Vb.Net
提问by Joy1979
I am trying to connect to database at window form level in Studio 2010/VB.Net. I am using two different databases in Sql 2008r2. One database I am using to populate records at form load event (run time) and when users pick their selection or any modification from that form, data should be updated to other database by instert, update, delete and save commands for user's future reference.
我正在尝试在 Studio 2010/VB.Net 的窗口级别连接到数据库。我在 Sql 2008r2 中使用了两个不同的数据库。我用来在表单加载事件(运行时)填充记录的一个数据库,当用户从该表单中选择他们的选择或任何修改时,数据应通过插入、更新、删除和保存命令更新到其他数据库以供用户将来参考。
Is it possible to connect with two different databases using one Connection String?
是否可以使用一个连接字符串连接两个不同的数据库?
Do I need to add two database name in below mentioned code somewhere?
我是否需要在下面提到的代码中添加两个数据库名称?
Imports System.Data.SqlClient
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
con.Close()
OR
或者
Should I use two database below somewhere?
我应该在下面的某个地方使用两个数据库吗?
Imports System.Data.SqlClient
Public Class NEW_PERSONAL_INFORMATION
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim con As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
con.Close()
End Sub
End Class
Apologies If I am seeking help in simple thing or not able to explain it enough.
道歉如果我在简单的事情上寻求帮助或无法解释清楚。
I would appreciate any help.
我将不胜感激任何帮助。
回答by ean5533
I can't tell what you're asking, so I'll give two answers.
我不知道你在问什么,所以我会给出两个答案。
If you're trying to connect to two different serversthen you'll need two different connection strings. In fact, you'll need two different SqlConnection
s, and you'll need to keep track of which connection is which.
如果您尝试连接到两个不同的服务器,那么您将需要两个不同的连接字符串。事实上,您将需要两个不同的SqlConnection
s,并且您需要跟踪哪个连接是哪个。
Dim con1 As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
Dim con2 As New SqlClient.SqlConnection("data source=ROOM310-40-2\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
If you're talking about multiple catalogs on the same server, then you don't need multiple connections. You can just specify the catalog name directly in your query:
如果您正在谈论同一服务器上的多个目录,那么您不需要多个连接。您可以直接在查询中指定目录名称:
SELECT MyColumn FROM MyDatabase1.dbo.MyTable;
SELECT MyColumn FROM MyDatabase2.dbo.MyTable;
回答by Gaurav
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASEm,Another Database Name,And SO ;Integrated Security=True")
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASEm,Another Database Name,And SO ;Integrated Security=True")