访问多个sqlserver的db [英] access to more than one db of sqlserver

查看:90
本文介绍了访问多个sqlserver的db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有一个ConnectionString







在ConnectionString中我们可以通过命令Initial定义db目录= MyDB;。

我想在SQL Server中访问2个数据库。

可以访问多个数据库的sqlserver

只有一个ConnectionString?





非常感谢

解决方案

< BLOCKQUOTE>是。单个打开的连接可以访问当前用户(用于打开连接的用户)拥有访问权限的所有数据库...

 SqlConnection oConnection =  new  SqlConnection(connectionString); 
oConnection.Open();
oConnection.ChangeDatabase( mydb1);
// 运行一些查询
oConnection.ChangeDatabase( mydb2);
// 运行更多查询
oConnection.Close();


是的,您可以使用单个连接到MS Server来处理多个数据库。



为了实现这一点,您需要将查询编写为:

  SELECT < FieldList> 
FROM [DatabaseName]。[dbo]。[TableName]





  USE  DatabaseName; 
SELECT < FieldList>
FROM [TableName]





但是,我需要警告你。从SQL Server获取数据的最佳方法是使用存储过程。


by only one ConnectionString

Hi

in ConnectionString we can define db by command "Initial Catalog=MyDB;".
I WANTS TO ACCESS TO 2 DB in SQL Server.
is it posible to access to more than one db of sqlserver
by only one ConnectionString?


Thanks Very Much

解决方案

Yes. A single opened connection can access all the databases the current user (the one used to open the connection) has access rights...

SqlConnection oConnection = new SqlConnection(connectionString);
oConnection.Open();
oConnection.ChangeDatabase("mydb1");
// run some query
oConnection.ChangeDatabase("mydb2");
// run some more query
oConnection.Close();


Yes, you can use single connection to MS Server to work with more than one database.

To able to achieve that, you need to write query as:

SELECT <FieldList>
FROM [DatabaseName].[dbo].[TableName]


or

USE DatabaseName;
SELECT <FieldList>
FROM [TableName]



But, i need to warn you. The best way to fetch data from SQL Server is to use stored procedures.


这篇关于访问多个sqlserver的db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆