发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
一:跨库查询
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 10sbut 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./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; } }
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
来自: 昵称10504424 > 《工作》
0条评论
发表
请遵守用户 评论公约
SQL?SERVER临时表实用大全
SERVER临时表实用大全_狗豆腐_新浪博客。SQL SERVER临时表实用大全。1.全局临时表和临时表的区别:视野不同。IF not (object_id(''tempdb.dbo.#Tmp_Detail'') is null) drop table...
成功.NET将SQL SERVER二进制转换为文件
sqlserver里服务器角色,服务器登录名,数据库用户,数据库角色,数据库架构的理解
sqlserver里服务器角色,服务器登录名,数据库用户,数据库角色,数据库架构的理解。多个架构,当某一个用户被转换成一种数据库角色的时...
java.io.IOException: Server returned HTTP response code: 403 for URL
IOException: Server returned HTTP response code: 403 for URL.java.io.因为服务器的安全设置不接受Java程序作为客户端访问,解决方案是设置客户端的User Agent.url = new URL("http://physics....
PowerPivot and Dynamic SQL Server Stored Procudure...
PowerPivot and Dynamic SQL Server Stored Procudure...· Copy and paste the following VBA scripts to change the SP_C...
教您sql server外键的设置和删除
--增加表mybbs(authorid)的sql server外键约束fk_mybbs_author,表mybbs中的authorid受表author中的主键id约束:begin transactionalter table dbo.mybbs add constraint fk_mybbs_author foreign key...
深入讲解数据库中User和Schema的关系
深入讲解数据库中User和Schema的关系深入讲解数据库中User和Schema的关系 假如我们想了解数据库中的User和Schema究竟是什么关系,首先必须了解一下数据库中User和Schema到底是什么概念。在SQL Se...
MS SQL Server 2008 新特性 -表作参数 C#编程
MS SQL Server 2008 新特性 -表作参数 C#编程。现在在MS SQL Server 2008可以完全实现这个功能了!其中完成这个功能的主要对象叫 User-Defined Table Type 自定义表类型。主要实现功能:在.NET 中生成一...
美创运维日记|SQL server 所有权链(上篇)
美创科技拥有强大的运维中心数据库服务团队,其中Oracle ACE 1人、OCM 10余人、数十名Oracle OCP、MySQL OCP、红帽RHCA、中间件weblogic...
微信扫码,在手机上查看选中内容