跨库查询(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 ...
随机推荐
- LoadRunner中的随机数
LoadRunner中的随机数 Action() { int i; ]; srand(time(NULL)); i=rand()%; lr_save_datetime("%m%d%H%M%S ...
- 伪分布式安装Hadoop
Hadoop简单介绍 Hadoop:适合大数据分布式存储与计算的平台. Hadoop两大核心项目: 1.HDFS:Hadoop分布式文件系统 HDFS的架构: 主从结构: 主节点,只有一个:namen ...
- Numpy的简单用法
Numpy的简单用法 import numpy as np 一.创建ndarray对象 列表转换成ndarray: >>> a = [1,2,3,4,5] >>> ...
- phpstorm 输入法中文不同步 phpstorm 输入法不跟随光标解决办法
win7系统新安装的phpstorm2017.2版本,试了很多输入法,要么是不显示候选次,要么是输入法候选词总是在屏幕右下角,没有跟随光标移动.百度很久,重要找到解决方案. 就是替换phpstorm安 ...
- iconfont 在项目中的简单使用
font-class引用 font-class是unicode使用方式的一种变种,主要是解决unicode书写不直观,语意不明确的问题. 与unicode使用方式相比,具有如下特点: 兼容性良好,支持 ...
- Bubbo的启动时检查
这个地方参考dubbo的官网,不是很难,为了使得文档的完整,也单独起一章. 1.默认 Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时, ...
- C++雾中风景7:闭包
本来说好要聊一聊命名空间的,因为最近在看C++lambda表达式的内容,所以借这个机会我们来好好聊一聊C++的闭包. 1.什么是闭包? 闭包(closure)是函数式编程的重要的语法结构. 闭包的概念 ...
- python获取文件属性
提供三段代码 1.os模块 import os def TestFileAttributes(): # This function is platform indepedent. statinfo = ...
- [ 转载 ] Mysql 远程连接+开放80和3306端口 常用配置
直接上方法: 首先配置CentOS下防火墙iptables规则: # vim /etc/sysconfig/iptables 向其中加入下列规则: -A INPUT -m state –state N ...
- CVE-2014-4113本地提权测试
CVE-2014-4113本地提权漏洞分析 By Netfairy 前言 2014年10月14日, Crowdstrike和FireEye发表了一篇文章, 描述了一个新的针对Windows的提权漏洞. ...