一:跨库查询

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. 【LOJ】#2046. 「CQOI2016」路由表

    题解 题面太长无法阅读系列-- 这里说的选择改变指的是在下面区间里碰上了一个更长的可匹配的地址,如果可匹配但是匹配长度没有当前的值大,那么不算改变 我们建一个可持久化的trie,查询的时候先在前\(a ...

  2. 【洛谷】P2179 [NOI2012]骑行川藏

    题解 感谢小迪给我讲题啊,这题小迪写挺好的我就不写了吧 小迪的题解 代码 #include <iostream> #include <cstdio> #include < ...

  3. ubuntu16.04编译安装mysql5.7

    1.安装编译依赖 sudo apt-get install make cmake gcc g++ bison libncurses5-dev build-essential 2.下载mysql5.7源 ...

  4. tensorflow初始化参数内存占满问题

    最近使用tensorflow构建了一个浅层的网络,初始化参数的时候发现两个1080ti 22G的显存马上占满 后来发现解决办法为:在创建session的时候把显存设置为自适应即可,如下 config ...

  5. sql中的if()和ifnull() 的用法和区别

    if() 把salary表中的女改成男,男改成女: update salary set sex = if( sex = '男','女','男'); if(true,a,b),  if(false,a, ...

  6. Windows 中打开 shell

    按住 Shift,单机鼠标右键"在当前目录打开命令窗口"

  7. Kubernetes网络模型概念

    Kubernetes网络模型 Kubernetes网络模型设计的一个基础原则是:每个Pod都拥有一个独立的IP地址,而且假定所有Pod都在一个可以直接连通的.扁平的网络空间中.所以不管它们是否运行在同 ...

  8. CentOS系统php5.6安装ImageMagick处理webp格式图片

    1.先安装webp yum install libwebp 2.编译安装ImageMagick 之前有过yum安装的先卸载 yum remove ImageMagick 我使用的是老版本ImageMa ...

  9. win7 64位安装pywin32

    先安装pywin32-218.win-amd64-py3.4.exe文件: 安装完成后,用CMD管理员模式进入\Python34\Scripts\目录 输入pywin32_postinstall.py ...

  10. Date与String间的转换

    一.Date-->String :格式化过程 1. DateFormat :String format(Date d) 2.SimpleDateFormat是继承DateFormat(抽象类)的 ...