跨数据库查询的实现

1.任务如图:

2.解决办法:

SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=master;Persist Security Info=True;User ID=sa;Password=111");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into clab_mid.dbo.patients select * from clab.dbo.patients where pat_id not in(select pat_id from clab_mid.dbo.patients)";
cmd.ExecuteNonQuery();

3.重点:

数据库要连接master :Initial Catalog=master

查询语句的表对象必须是:数据库名.dbo.表名

 

posted @ 2014-03-13 11:51  talentzemin  阅读(434)  评论(0编辑  收藏  举报