使用另一个数据库的值从一个数据库中搜索 [英] Search from one database using the values of another database
本文介绍了使用另一个数据库的值从一个数据库中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据库,我正在搜索所有来自database1的custId,并且我需要从数据库2中检索完整的信息。
作为custid将有多个值(比如说我有10个custid,我需要数据库2中的所有客户信息。
这是我的C#代码:
I have two database first I am searching all custId from database1 and for all that custId I need to retrive their complete infirmation from database 2.
As custid will have more than one value ( say 10 custid I have and I need all customer info from database 2.
Here is my C# code:
using System.Data.SqlClient;
// .....
string queryString = "Select custID from Table1";
String TempCustID;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
TempCustID = reader[0].ToString();
// Here I need to connect to other database and use TempCustID
// variable to search in a Table in DB2
// And display values in datagridview
}
}
reader.Close();
}
推荐答案
通常 join语句 [ ^ ]用于这种情况。
Usually join statement[^] is used in such of situations.
Select t1.custID, t2.TempCustId
From [dbo1].Table1 AS t1 INNER JOIN [dbo2].Table1 AS t2 ON t1.CustId = t2.TempCustId
以上查询返回记录集,其中 CustId
字段等于 TempCustId
字段。有关详细信息,请参阅: SQL联接的可视化表示 [ ^ ]
Above query returns recordset where CustId
field is equal to TempCustId
field. For further information, please see: Visual Representation of SQL Joins[^]
这篇关于使用另一个数据库的值从一个数据库中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文