跨库查询(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,如需转载请自行联系原作者

相关文章
|
5月前
|
SQL 数据库
数据库SQL Server 6-7 章(数据与操作查询)
数据库SQL Server 6-7 章(数据与操作查询)
92 0
|
SQL 数据库 索引
|
SQL Go 数据库
SQL Server删除distribution数据库二
原文:SQL Server删除distribution数据库二     以前总结过一遍博文SQL Server删除distribution数据库,里面介绍了如何删除distribution数据库。今天介绍一个删除distribution的特殊案例,     在这之前,我不知道这个服务器上的Replication被如何折腾过,在SSMS管理界面的Local Publications和Local Subscriptions里面已经看不到任何关于发布、订阅相关的内容。
793 0
|
SQL Go 数据库
SQL Server删除distribution数据库
原文:SQL Server删除distribution数据库   在数据库服务器删除复制(发布订阅)后,如何删除掉数据库distribution呢?如果你通过SSMS工具去删除数据库distribution,你会发现根本没有删除选项。
843 0
|
SQL 存储
sql server 2005 链接服务器:未将服务器 配置为用于 RPC
原文:sql server 2005 链接服务器:未将服务器 配置为用于 RPC  RPC: 远程过程调用(Remote Procedure Calls) --LinkedServer.Rpc 属性: --获取或设置 Boolean 属性值,该值指定链接服务器是否支持远程过程调用 (RPC)。
1725 0
|
SQL 索引
sql server递归查询
1、既然要谈到sql,数据库表是必须的   2、数据结构     3、获取某个节点的所有子节点     传统的写法(sql2000) 很麻烦,暂且就不写了     来看看CTE的写法     CREATE PROC sp_getTreeById(@TreeId int) A...
1384 0