博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
跨库查询(OpenDataSource)与链接服务器(Linking Server)
阅读量:6160 次
发布时间:2019-06-21

本文共 2232 字,大约阅读时间需要 7 分钟。

一:跨库查询

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的效率问题的一些描述:

 

四:代码实现

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://wehfa.baihongyu.com/

你可能感兴趣的文章
【转】灵活运用 SQL SERVER FOR XML PATH
查看>>
WCF角色服务
查看>>
常用sql001_partition by 以及 row_number()和 dense_rank()和rank()区别
查看>>
dev c++ Boost库的安装
查看>>
[LeetCode] 518. Coin Change 2
查看>>
react+百度地图实现自定义图标
查看>>
Java编译期优化思维导图
查看>>
前端自动化开发环境
查看>>
基于 Generator 和 Iterator 的惰性列表
查看>>
JS -- http、https地址自动检测并添加为链接
查看>>
JS正则表达式
查看>>
关于vue中next和Tick(nextTick)的一点理解
查看>>
聊聊rocketmq的FileAppender
查看>>
Vue登录注册,并保持登录状态
查看>>
Asciidoctor Maven插件使用
查看>>
RBAC-基于角色的权限管理
查看>>
ES6 札记:函数
查看>>
如何进行多云环境中的数据管理?
查看>>
Vuex源码阅读分析
查看>>
samba服务
查看>>