将数据从两个数据库添加到一个gridview [英] Add data from two databases into one gridview

查看:90
本文介绍了将数据从两个数据库添加到一个gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的问题:



DB1有两个表格,我需要从中提取数据

用户

FName

LName

电子邮件

UserId

用户名



UserData

角色

部门

UserId



数据库2有一个表格,我需要从中提取数据

登录 >

CreateDate

UserName



我的gridview最终需要显示以下内容: p>

fName lName电子邮件部门角色CreateDate



我需要将这三个表格连接成1个gridview。我无法控制数据库,所以我无法创建任何新表。我的问题是,我似乎无法成功链接这些。关于如何解决这个问题的任何想法?

创建一个查看一个简单的 Join 将它用作 DataSource 用于 GridView 。然后,编辑 GridView 中的列以仅显示您感兴趣的字段,或者使您的 View 仅返回那些专栏。您的选择可能是这样的:

  CREATE VIEW myview AS 
SELECT u.fname,
u .lname,
u.email,
ud.department,
ud.role l.CreateDate
FROM db1.dbo.USER u
INNER JOIN db1.dbo。 userdata ud ON u.userid = ud.userid
INNER JOIN db2.dbo.login l ON u.username = l.username

然后你可以像这样使用它:

  SELECT * FROM myview 
code>

以及更高级的查询:

  SELECT * FROM myview WHERE userid = 1 

可选阅读:< a href =http://technet.microsoft.com/en-us/library/ff772782.aspx =nofollow>创建链接服务器


I have two different databases, and want to take data from both databases and insert it into a gridview.

Here is my problem:

DB1 has two tables from which I need to pull data

User
FName
LName
Email
UserId
UserName

UserData
Role
Department
UserId

DB 2 has 1 table from which i need to pull data

Login
CreateDate
UserName

My gridview ultimately needs to display the following:

fName lName Email Department Role CreateDate

I somehow need to connect these three tables into 1 gridview. I do not have control over the database, so I cannot create any new tables. My problem is that I cannot seem to link these successfully. Any ideas on how to go about tackling this?

解决方案

Create a View with a simple Join use this as your DataSource for your GridView. Then either edit the columns on your GridView to display only the fields you are interested in, or make your View return only those columns. Your select could be something like this:

CREATE VIEW myview AS
SELECT u.fname,
       u.lname,
       u.email,
       ud.department,
       ud.role l.CreateDate
FROM db1.dbo.USER u
INNER JOIN db1.dbo.userdata ud ON u.userid = ud.userid
INNER JOIN db2.dbo.login l ON u.username = l.username

Then you can use it like:

SELECT * FROM myview

And more advanced queries:

SELECT * FROM myview WHERE userid=1

Optional Reading: Create Linked Servers

这篇关于将数据从两个数据库添加到一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆