一:跨库查询

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)的更多相关文章

  1. 如何使用SQL SERVER数据库跨库查询

    SQL Server中内置了数据库跨库查询功能,下面简要介绍一下SQL Server跨库查询.首先打开数据源码:OPENDATASOURCE不使用链接的服务器名,而提供特殊的连接信息,并将其作为四部分 ...

  2. SqlServer跨库查询

    由于业务的拆分,数据库拆分为两种作用: 汇总数据库(Master,头节点数据库), 子节点数据库(Compute Node,计算子节点数据库) 这样,就设计到子节点访问头节点数据库中的某张汇总表,这种 ...

  3. ACCESS-如何多数据库查询(跨库查询)

    测试通过:ACCESSselect * from F:\MYk.mdb.tablename说明:1.查询语句2.来原于哪(没有密码是个路径)3.查询的表名 ====================== ...

  4. SQLServer跨库查询--分布式查询

    出处:http://www.cnblogs.com/doosmile/archive/2012/03/16/2400646.html --用openrowset连接远程SQL或插入数据 --如果只是临 ...

  5. SQLServer跨库查询--分布式查询(转载)

    --用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset('SQLOLEDB' ,'sq ...

  6. oracle使用dblink跨库查询的例子

    本文介绍了oracle数据库使用dblink进行跨库查询的方法,oracle dblink跨库查询教程,需要的朋友参考下.   oracle dblink跨库查询 方法一:首先,创建数据库链接: 复制 ...

  7. EF 跨库查询

    原因:最近公司项目,遇到一个ef跨库查询的问题.(只是跨库,并不是跨服务器哈) 主要我们的一些数据,譬如地址,城市需要查询公共资料库. 但是本身我的程序设计采用的是ef框架的.因此为这事花费了1天时间 ...

  8. SQL Server 跨库查询

    1. 开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句: reconfigure reconfigure 2. 跨库查询操作 select * from ...

  9. mysql 跨库查询问题

    MySQL实现跨服务器查询 https://blog.csdn.net/LYK_for_dba/article/details/78180444 mysql> create database l ...

随机推荐

  1. USACO 6.3 Fence Rails(一道纯剪枝应用)

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...

  2. docker 安装错误Transaction Check Error

    安装docker提示:Transaction Check Error: file /usr/bin/docker from install of docker-io-1.7.1-2.el6.x86_6 ...

  3. CSUOJ 1021 组合数末尾的零 二进制

    Description 从m个不同元素中取出n (n ≤ m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数.组合数的计算公式如下: C(m, n) = m!/((m - n)!n! ...

  4. POJ - 3111 K Best 0-1分数规划 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 12812   Accepted: 3290 Case Time ...

  5. 【CF148D】 Bag of mice (概率DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. codevs 2577 医院设置

    2577 医院设置 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold   题目描述 Description 设有一棵二叉树,如下图 其中,圈中数字表示结点居民的人口.圈边 ...

  7. [BZOJ4372]烁烁的游戏(动态点分治+线段树)

    和[BZOJ3730]震波几乎一样,每个点建两棵线段树分别代表它的管辖范围内以它为LCA的路径的贡献和它对父亲的贡献. 注意点分树上的点的距离在原树上不单调,所以不能有若距离超出限制就break之类的 ...

  8. [POI2015]Pustynia

    [POI2015]Pustynia 题目大意: 给定一个长度为\(n(n\le10^5)\)的正整数序列\(a\),每个数都在\(1\)到\(10^9\)范围内,告诉你其中\(s\)个数,并给出\(m ...

  9. python开发_thread_布朗运动

    这篇blog是非常有趣的,是的,他非常有趣 下面我将给大家介绍有关python中thread来实现布朗运动的一个例子 下面是运行效果: ================================ ...

  10. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...