5

I have a database with two tables. Both of these tables are related and have the same key field. For example, both of them have rows of data corresponding to ISBN = 12345, but the two tables have differing data about that ISBN.

So, I'm trying to figure out how to display data from both tables into one dataGridView. I have tried some SQL commands I found online, but it looks like commands in C# might differ from normal SQL queries.

Suppose table1 has these fields: ISBN, color, size and table2 has the fields ISBN, weight.

I need a way to display ISBN, color, size, weight in my datagrid view. I think I will have to somehow do this with an adapter. I am able to connect and do queries on the tables individually, and show that data in my datagridview, but I can't figure out how to mix data from two separate tables.

If you have a good resource I can read about this I'd love to have it, my google-fu is failing me.

Here's an example of something I can do now with my database:

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'database1DataSet.Book' table. You can move, or remove it, as needed.
        this.bookTableAdapter.Fill(this.database1DataSet.Book);
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"C:\Users\Geoff\Documents\cs 351\Database1.accdb" + ";Persist Security Info=False;";
        OleDbConnection conn = new OleDbConnection(connectionString);
        string query = "select * from Book where ISBN = 12345";
        OleDbCommand com = conn.CreateCommand();
        com.CommandText = query;
        OleDbDataAdapter adapter = new OleDbDataAdapter(com);
        DataSet data = new DataSet();
        conn.Open();
        adapter.Fill(data);

        conn.Close();
        dataGridView1.DataSource = data.Tables[0];
    }

So, essentially, I'd like to do what I've done above, but I want to include the data from a different table too. The other table also has a key field ISBN, and it contains values of ISBN that match the first table.

Zano
  • 2,595
  • 27
  • 33
jeff
  • 968
  • 1
  • 6
  • 13
  • 2
    Hi @Jeff and welcome to StackOverflow. While we like to help, we also like to see what you've got so far. Can you give us an example of some data and how it exists currently? Pay attention to showing the problematic parts. We could simply say "do a left join" but that doesn't help you. – jcolebrand Nov 28 '10 at 21:20
  • Agreed, if you're working with databases in C# there is a whole universe of possible solutions, each with it's own strengths and weaknesses. You could for example investigate a LINQ to SQL or Entity Framework solution rather than go for raw SQL. You might have seen examples of these technologies which is why you think 'commands in C# might differ from normal SQL queries'. Be assured C# supports 'normal' SQL via ADO.net, but it support much more, including queries as first class language objects via LINQ. You are embarking on an exciting journey! :) – James Gaunt Nov 28 '10 at 21:29
  • Besides my example I just posted, I definitely agree with James. There are a lot of possibilities, and you should do some research and trial to see which solution is best in your case. – KBoek Nov 28 '10 at 21:38
  • Ok, i updated my original post, sorry if i was a little vague. – jeff Nov 28 '10 at 21:59
  • @drachenstern here's what is in the table 'Book'. http://i.imgur.com/WIOK8.png This is just data I made up. – jeff Nov 28 '10 at 22:09
  • @Jeff which table has the multiple-per-ISBN? – jcolebrand Nov 28 '10 at 22:22
  • @Jeff can you visit http://chat.stackoverflow.com/rooms/5/the-so-tavern-general and us have a brief chat? Might be faster than this comment-to-comment (working on getting you the requisite +20) – jcolebrand Nov 28 '10 at 22:24
  • Doesn't look like i can chat there yet, here's another pic showing the contents of my two tables and their relationship in access: http://i.imgur.com/gOdB0.png – jeff Nov 28 '10 at 22:44

3 Answers3

3

Look into the use of JOIN to return the results from two tables JOINed together ON some common value

See Also

There's nothing limiting this to C# or OLEDB -- it's basic SQL.


For the specifics of what you're asking a query might look like the following:

SELECT T1.ISBN, T1.color, T1.size, T2.weight
FROM table1 T1
  INNER JOIN table2 T2
    ON T1.ISBN = T2.ISBN
WHERE ISBN = '12345';

(There's no need to alias table1 as T1 -- I just did that as an example; in more complicated queries with longer table names, you might not want to repeat the table name all the time)

  • since ISBN occurs in both tables, it must be explicitly qualified in your field-selections; either T1 or T2 can be used, as they are identical
  • since color, size and weight each occur in only one table, they do NOT need to be qualified -- but it doesn't hurt.
Community
  • 1
  • 1
Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
1
var query       = "SELECT t1.isbn, t1.color, t1.size, t2.weight FROM table1 t1 JOIN table2 t2 ON t2.isbn = t1.isbn";
var connection  = new System.Data.SqlClient.SqlConnection("your SQL connection string here");
var dataAdapter = new System.Data.SqlClient.SqlDataAdapter(query, connection);
var dataSet     = new System.Data.DataSet();

dataAdapter.Fill(dataSet);

yourGridView.DataSource = dataSet;
yourGridView.DataBind();

This is one of many solutions. I think the code might be faster if you create an in-memory DataTable and use an SqlDataReader, but the sample above is simpler.

When working with MSSQL databases, you normally use the System.Data.SqlClient classes. If you - for whatever reason - use OleDb, pick the corresponding objects from the System.Data.OleDb namespace.

KBoek
  • 5,794
  • 5
  • 32
  • 49
  • This syntax seems different. I'm not sure what 't1.color' means. Is this just a different identifier for table1? or? – jeff Nov 28 '10 at 23:12
0

You can query records from both tables using UNION ALL

SELECT 'In table 1', book_author, book_title, book_isbn 
FROM books
WHERE book_isbn = '67890' 
UNION ALL
SELECT 'In table 2', othertable_author, othertable_title, othertable_isbn 
FROM othertable
WHERE othertable_isbn = '67890'

of course you'll need to manually fill the '67890' in both places using whatever method is more convenient in your situation.

dschulz
  • 4,666
  • 1
  • 31
  • 31