跨库查询(OpenDataSource)与链接服务器(Linking Server)
一:跨库查询
Openrowset/opendatasource() is an ad-hoc method to access remote server's data. So, if you only need to access the remote server's data once and you do not to persist the connection info, this would be the right choice. Otherwise, you should create a linked server.
开启'Ad Hoc Distributed Queries'
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
关闭'Ad Hoc Distributed Queries'
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
SQL如下:
select * from [User] a
left join OPENDATASOURCE(
'SQLOLEDB',
'Data Source=192.168.1.20;User ID=sa;Password=yhbj'
).Kitty2.dbo.Question b on a.Id = b.UserId
结果:

二:链接服务器
Linked server is a persistent registration for the remote server. This allows you to define the connection property once and be able to use the server "alias" over and over again.
STEP1:

STEP2:
如果是在同一个局域网内,不妨直接选中“SQLSERVER”链接,

STEP3:

STEP4:

三:OpenDataSource VS Linking Server
有这样一个实例:
using linked server
SELECT * FROM mylinkedserver.[mydatabase].[dbo].[mytable]
requires 10s
but when using
SELECT * FROM OPENDATASOURCE('SQLNCLI','Data Source=myserver;User ID=sa;Password=xxxxxx').[mydatabase].[dbo].[mytable]
it lasts for 10 mins and still continue
其它基于LINK SERVER的效率问题的一些描述:
http://www.sql-server-performance.com/2007/linked-server/
四:代码实现
static void Main(string[] args)
{
var x = Query("select * from [User] a left join [192.168.1.20].Kitty2.dbo.Question b on a.Id = b.UserId ");
Console.WriteLine(x.Tables[0].Rows.Count);var y = Query("select * from [User] a"
+ " left join OPENDATASOURCE("
+ " 'SQLOLEDB','Data Source=192.168.1.20;User ID=sa;Password=yhbj' "
+ " ).Kitty2.dbo.Question b on a.Id = b.UserId ");
Console.WriteLine(y.Tables[0].Rows.Count);
}private static string connectionString = @"Data Source=192.168.1.19;Initial Catalog=Kitty1;Integrated Security=False;User ID=sa;Password=yhbj;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
DataSet ds = new DataSet();
command.Fill(ds, "ds");
return ds;
}
}
参考:
http://www.cnblogs.com/EasonWu/archive/2012/09/26/2704018.html
http://msdn.microsoft.com/en-us/library/ms188279.aspx
http://msdn.microsoft.com/en-us/library/ms188721.aspx
http://msdn.microsoft.com/en-us/library/ms188477.aspx
http://msdn.microsoft.com/en-us/library/aa259589%28v=sql.80%29.aspx
http://msdn.microsoft.com/en-us/library/aa933295%28v=sql.80%29.aspx
http://msdn.microsoft.com/en-us/library/aa259581%28v=sql.80%29.aspx
http://www.codeproject.com/Articles/35943/How-to-Config-Linked-Servers-in-a-Minute#
跨库查询(OpenDataSource)与链接服务器(Linking Server)的更多相关文章
- 如何使用SQL SERVER数据库跨库查询
SQL Server中内置了数据库跨库查询功能,下面简要介绍一下SQL Server跨库查询.首先打开数据源码:OPENDATASOURCE不使用链接的服务器名,而提供特殊的连接信息,并将其作为四部分 ...
- SqlServer跨库查询
由于业务的拆分,数据库拆分为两种作用: 汇总数据库(Master,头节点数据库), 子节点数据库(Compute Node,计算子节点数据库) 这样,就设计到子节点访问头节点数据库中的某张汇总表,这种 ...
- ACCESS-如何多数据库查询(跨库查询)
测试通过:ACCESSselect * from F:\MYk.mdb.tablename说明:1.查询语句2.来原于哪(没有密码是个路径)3.查询的表名 ====================== ...
- SQLServer跨库查询--分布式查询
出处:http://www.cnblogs.com/doosmile/archive/2012/03/16/2400646.html --用openrowset连接远程SQL或插入数据 --如果只是临 ...
- SQLServer跨库查询--分布式查询(转载)
--用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset('SQLOLEDB' ,'sq ...
- oracle使用dblink跨库查询的例子
本文介绍了oracle数据库使用dblink进行跨库查询的方法,oracle dblink跨库查询教程,需要的朋友参考下. oracle dblink跨库查询 方法一:首先,创建数据库链接: 复制 ...
- EF 跨库查询
原因:最近公司项目,遇到一个ef跨库查询的问题.(只是跨库,并不是跨服务器哈) 主要我们的一些数据,譬如地址,城市需要查询公共资料库. 但是本身我的程序设计采用的是ef框架的.因此为这事花费了1天时间 ...
- SQL Server 跨库查询
1. 开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句: reconfigure reconfigure 2. 跨库查询操作 select * from ...
- mysql 跨库查询问题
MySQL实现跨服务器查询 https://blog.csdn.net/LYK_for_dba/article/details/78180444 mysql> create database l ...
随机推荐
- 继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类。 (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法。 (3)子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量不能被继承。 (4)子类中定义的成员方法,并且这个方法的名字返回类型,以及参数个数和类型与父类的某个成员方法完全相同,则父类的成员方法不能被继承。 分析以上程
继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法.(3)子类中定义的成员变量和父类中定义的 ...
- pct_free
SQL> select table_name,pct_free,pct_used from user_tables; TABLE_NAME PCT_FREE PCT_USED---------- ...
- 003 Scipy库简介
参考文档补充原本的文档: https://www.cnblogs.com/mrchige/p/6504324.html 一:原本的简单介绍 1.Scipy库 Scipy库是基于python生态的一款开 ...
- php7的新特性
新增操作符1.??$username = $_GET['user'] ?? '';$username = isset($_GET['user']) ? $_GET['user'] : 'nobody' ...
- 洛谷 P2042 [NOI2005]维护数列-Splay(插入 删除 修改 翻转 求和 最大的子序列)
因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客. 先直接上题目然后贴代码,具体讲解都写代码里了. 参考的博客等的链接都贴代码里了,有空再好好写. P2042 [NOI2005]维护数列 ...
- 59. 螺旋矩阵 II
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- vue开发computed,watch,method执行的先后顺序
1#computed:计算属性将被混入到 Vue 实例中.所有 getter 和 setter 的 this 上下文自动地绑定为 Vue 实例. 2#methods:methods 将被混入到 Vue ...
- CodeForces - 725D Contest Balloons 贪心
D. Contest Balloons time limit per test 3 seconds memory limit per test 2 ...
- Socket 编程之 TCP 实现
前几天介绍了计算机网络的一些概念,并介绍了几个协议.下面就说说 Java 中的 Socket 编程,服务器和客户端是如何通信的呢? 首先要介绍一下 Socket ,我们知道在 TCP/IP 协议簇中, ...
- 转Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...