简体   繁体   English

使用Data Reader访问两个数据库

[英]Accessing two databases using Data Reader

I am trying to develop an application using ADO.NET. 我正在尝试使用ADO.NET开发应用程序。 I have two tables in two different databases. 我在两个不同的数据库中有两个表。 Each of them have a portion of the complete data. 他们每个人都有一部分完整的数据。 Now I need to write a query such that I am able to fetch the complete record from the two tables. 现在我需要编写一个查询,以便我能够从两个表中获取完整的记录。 As an example say table 1 has Index_no,emp_ID and contact no. 例如,表1具有Index_no,emp_ID和联系号。 Table 2 has index_no, emp_name, salary and dept. 表2包含index_no,emp_name,salary和dept。 The index_no is same for the same record portion in each table. index_no对于每个表中的相同记录部分是相同的。 Following is the code to fetch all records where salary <20000. 以下是获取薪水<20000的所有记录的代码。

sqlCmd2 = new SqlCommand("SELECT * FROM table1 WHERE Index_No =@Index_No",
                         TestCon);
int i = 0; 
while (reader.Read()) {
    ListBox1.Items.Add(reader.GetInt32(0) + "  -   " + 
      reader.GetString(1) + "  -   " + reader.GetString(2));
    //  Name.Text += reader["Name"] + "<br />"; ;
    // Depart.Text += reader["Depart"] + "<br />"; ;
    array1[i] = reader.GetInt32(0);
    i++;
}

sqlCmd2.Parameters.Add("@Index_No", System.Data.SqlDbType.Decimal);
i = 0;
do {
    sqlCmd2.Parameters["@Index_No"].Value = array1[i];
    reader1 = sqlCmd2.ExecuteReader();
    reader1.Read();
    ListBox2.Items.Add(reader1.GetString(1));
    i++;
} while (i < array1.Length);

The problem with this is that I am getting only one record info from table2 while for table 1 I get all the desired record info. 这个问题是我从table2只得到一个记录信息,而对于表1,我得到了所有想要的记录信息。 The do-while loop seems to terminate after one iteration only. do-while循环似乎仅在一次迭代后终止。

It depends, what two different databases means. 这取决于两个不同的数据库意味着什么。 If the two databases run on the same SQL-Server, it is easy to access the table of the other database 如果两个数据库在同一个SQL Server上运行,则可以轻松访问另一个数据库的表

 SELECT * FROM OtherDb.dbo.TableOnOtherDb

If not, I would suggest you to create a database link between the two databases (see SO question How do I create and query linked database servers in SQL Server? ). 如果没有,我建议你在两个数据库之间创建一个数据库链接(参见SO问题如何在SQL Server中创建和查询链接数据库服务器? )。 Then you can access the other table as above. 然后您可以访问上面的其他表。

In both cases you can use a JOIN query to join the two tables. 在这两种情况下,您都可以使用JOIN查询来连接这两个表。 Then you will need only one data reader 那么您只需要一个数据读取器

SELECT
    A.Index_no, A.emp_ID, A.contact_no,
    B.emp_name, B.salary, B.dept
FROM
    table1 A
    INNER JOIN table2 B
        ON A.Index_no = B.Index_no
WHERE
    B.salary < 20000
ORDER BY
    B.emp_name

UPDATE UPDATE

If the number of involved records is small, then querying the second table only once by using a IN clause would be far more efficient, if you cannot link the two servers for some reason. 如果涉及的记录数量很少,那么如果由于某种原因无法链接这两个服务器,则使用IN子句仅查询第二个表将会更有效率。

SELECT *
FROM table1
WHERE Index_no IN (4,12,17,30,112,167)

You would create this SQL statement like this 您可以像这样创建此SQL语句

string[] stringArray = array1
    .Select(i => i.ToString())
    .ToArray();
string list = String.Join(",", stringArray);
string sql = "SELECT * FROM table1 WHERE Index_No IN (" + list + ")";

The idiomatic way of doing the second loop would be (not do while ) 做第二个循环的惯用方法是(不要do while

for (int i = 0; i < array1.Length; i++) {
    ...
}

Why don't you write a stored procedure which joins both the ables and gives back the results? 为什么不编写一个连接两个存储的存储过程并返回结果? You can connect to the second db using linked server or you can refer the second table using the dbname.tablename format. 您可以使用链接服务器连接到第二个数据库,也可以使用dbname.tablename格式引用第二个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM