跨库查询(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 ...
随机推荐
- 纯CSS仿制Google女生节Doodle
看到google今天的女生节Doodle,自己用纯css仿制一个,送给老妈.老婆.女儿. 大家可以点这里在线观看效果,点这里下载收藏效果. 实现原理 1.利用checkbox侦听处理单击事件. 2.单 ...
- Python下opencv使用笔记(图像的平滑与滤波)
对于图形的平滑与滤波,但从滤波角度来讲,一般主要的目的都是为了实现对图像噪声的消除,增强图像的效果. 对于2D图像可以进行低通或者高通滤波操作 低通滤波(LPF):有利于去噪,模糊图像 高通滤波(HP ...
- thinkphp5.0返回插入数据id
添加数据后如果需要返回新增数据的自增主键,可以使用getLastInsID方法: Db::name('user')->insert($data); $userId = Db::name('use ...
- 码云Android项目构建注意事项(转载)
1.ant项目 build.xml必须位于项目根目录. 2.maven项目 pom.xml必须位于项目根目录. 3.gradle项目 由于gradle的配置灵活,我们做了一些规范,并且增加了一下机制来 ...
- tomcat+serlet+eclipse环境按键
---恢复内容开始--- 1. tomcat环境搭建 安装向导:http://www.runoob.com/jsp/eclipse-jsp.html 1. tomcat启动一闪而过,需要配置 JAVA ...
- CSUOJ 1560 图书管理员的表白方式
Description 小V是中南大学图书馆的图书管理员,每天要整理很多同学们还回来的书.久而久之,他认识了很多常来图书馆的同学,比如说小L.简而言之吧,就是小V喜欢上了小L,并且想在下一次她来还书的 ...
- [转载] 你所不知道的TIME_WAIT和CLOSE_WAIT
前言 本文转载自 https://mp.weixin.qq.com/s/FrEfx_Yvv0fkLG97dMSTqw.很久前看到Vincent Bernat在博客中写了一遍关于TCP time-wai ...
- grunt自动化
1.安装模块 npm install grunt -g npm install grunt-cli -g #! --save-dev 既会把模块安装到项目node_modules下,也会安装到依赖文件 ...
- BZOJ.4516.[SCOI2016]幸运数字(线性基 点分治)
题目链接 线性基可以\(O(log^2)\)暴力合并.又是树上路径问题,考虑点分治. 对于每个点i求解 LCA(u,v)==i 时的询问(u,v),只需求出这个点到其它点的线性基后,暴力合并. LCA ...
- 【提权思路】绕过SecureRDP限制远程连接
工具可以在百度上下载 直接步入正题 配置好的SecureRDP是限制远程登录的用户 原理是判断来访的计算机名是否在白名单中 如果不在,便出现如上图所示 网上也有绕过方法(https://weibo.c ...