如何在C#中合并数据库表? [英] How to merge tables of a database in c#?

查看:130
本文介绍了如何在C#中合并数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server数据库中有两个表.第一个(db1)包含诸如用户名,密码,地址,电话号码等信息,而另一个(db2)包含诸如用户名,教育程度,关键技能,爱好等信息.用户名是两者之间的通用密钥.
Ive在asp.net网站上做了两页注册.第一个表单包含用户名,密码,地址,电话号码等字段.第二种形式具有教育,关键技能,爱好等领域.代码在C#中.在第二种形式的(C#)后面的代码中,我想比较一下如果db1中的用户名等于db2中的用户名,则在table2(db2)中填写详细信息.如何完成这项任务?救命!!!
Thnks

I have two tables in SQL server database. The first one(db1) contains information such as username, password, address, phone no etc and the other one(db2) contains information such as username, education, key skills, hobbies etc. The username is the common key between the two.
Ive made two pages in asp.net website for registration. First one has a form with fields username,password, address, phone no. The second one has a form with fields as education, key skills, hobbies. The code is in C#.In the code behind(C#) of second form, I want to compare that if the username from db1 is equal to username from db2, then fill in the details in the table2(db2). How to accomplish this task ? Help!!!
Thnks

推荐答案

在使用SQL Server的情况下,我会将信息传递给存储过程,然后使用T-SQL进行操作

1.比较两个数据库中的用户名,即

AS you are using SQL Server I would pass the information to a stored procedure and then using T-SQL I would do

1. Compare the usernames from the 2 databases i.e.

declare @UserName VARCHAR(50) = null

select @UserName = a.username
--tableName is the name of the table in database1
from tableName b inner join database2.dbo.TableName b
on a.username = b.username



2.如果该名称存在于数据库2中,则执行insert



2. If the name exists in database 2 then do the insert

if @UserName is not null
begin
 insert into database2.dbo.tablename
 select columnnames
 from tablename
end



进一步阅读
创建存储过程 [使用ADO.NET调用存储过程 [



Further Reading
Create Stored Procedure[^]

Calling Stored Procedure using ADO.NET[^]
This article is in C# but it is very easy to convert


您可以使用以下代码在另一个数据库连接中查询另一个数据库:
You can query another database within another database connection with the following code:
select * from database2.dbo.tablename -- execute from database1 connection


这篇关于如何在C#中合并数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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