Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7709913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:58:53+00:00 2026-06-01T00:58:53+00:00

I am trying to develop an application using ADO.NET. I have two tables in

  • 0

I am trying to develop an application using 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. Table 2 has index_no, emp_name, salary and dept. The index_no is same for the same record portion in each table.
Following is the code to fetch all records where salary <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. The do-while loop seems to terminate after one iteration only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:58:54+00:00Added an answer on June 1, 2026 at 12:58 am

    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

     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?). Then you can access the other table as above.

    In both cases you can use a JOIN query to join the two tables. 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

    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.

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

    You would create this SQL statement like this

    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)

    for (int i = 0; i < array1.Length; i++) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop a Silverlight 4 application using C# 4.0. I have
I have been trying to develop a Java ME application using WTK 2.5.2 (Java
We are two friends trying to develop an application on local network using symfony
I'm trying to develop a cross-platform application using C++ with boost. I typically program
I am trying to develop an image gallery application using Qt Framework. The application
Currently, I'm trying to develop a rich internet application using Flash Builder 4 Beta
I'm starting to develop a facebook application using Django. I'm trying to choose the
I am trying to develop an iphone application using sencha framework .I need to
I am trying to develop one simple application using Java, Flex and Blazeds. I
I am trying to develop a Wicket-based web application using Hibernate at the model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.