跨库查询(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

结果:

image

 

二:链接服务器

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:

image

STEP2:

如果是在同一个局域网内,不妨直接选中“SQLSERVER”链接,

image

STEP3:

image

STEP4:

image

 

三: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/luminji/p/3445407.html,如需转载请自行联系原作者

相关文章
|
1月前
|
存储 数据安全/隐私保护 索引
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
128 1
|
4月前
|
开发框架 .NET 数据库连接
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
665 0
|
12天前
|
弹性计算 前端开发 Java
使用阿里云 mqtt serverless 版本超低成本快速实现 webscoket 长链接服务器
使用阿里云 MQTT Serverless 可轻松实现弹性伸缩的 WebSocket 服务,每日成本低至几元。适用于小程序消息推送的 MQTT P2P 模式。前端需注意安全,避免 AK 泄露,采用一机一密方案。后端通过调用 `RegisterDeviceCredential` API 发送消息。示例代码包括 JavaScript 前端连接和 Java 后端发送。
102 0
|
1月前
|
存储 Windows
windows server 2019 云服务器看不见硬盘的解决方案
windows server 2019 云服务器看不见硬盘的解决方案
|
1月前
|
数据安全/隐私保护 Windows
Windows Server 各版本搭建终端服务器实现远程访问(03~19)
左下角开始➡管理工具➡管理您的服务器,点击添加或删除角色点击下一步勾选自定义,点击下一步蒂埃涅吉终端服务器,点击下一步点击确定重新登录后点击确定点击开始➡管理工具➡计算机管理,展开本地用户和组,点击组可以发现有个组关门用来远程登录右键这个组点击属性,点击添加输入要添加的用户名,点击确定添加成功后点击确定打开另一台虚拟机(前提是在同一个局域网内),按 WIN + R 输入 mstsc 后回车输入 IP 地址后点击连接输入用户名及密码后点击确定连接成功!
32 0
|
1月前
|
数据可视化 网络安全 Windows
下载安装MobaXterm并链接服务器的操作方法
【2月更文挑战第13天】本文介绍在Windows电脑中,下载、配置MobaXterm软件,从而连接、操作远程服务器的方法~
下载安装MobaXterm并链接服务器的操作方法
|
1月前
|
存储 Ubuntu 网络安全
|
2月前
|
文件存储 Windows
Windows server 2012 服务器挂载NAS盘
Windows server 2012 服务器挂载NAS盘
|
3月前
|
Linux 网络安全 数据安全/隐私保护
centos宝塔远程服务器怎么链接?
【1月更文挑战第6天】centos宝塔远程服务器怎么链接?
188 2
|
4月前
|
Shell 数据安全/隐私保护 Windows
Windows Server【开机启动和任务计划程序】实现服务器重启后项目自启动(Windows Server 任务计划程序无法执行问题处理)
Windows Server【开机启动和任务计划程序】实现服务器重启后项目自启动(Windows Server 任务计划程序无法执行问题处理)
159 0

热门文章

最新文章