简体   繁体   中英

Retrieving Data from a query on multiple tables in C#

Alrighty, so I have a C# program I'm working on and I'm running into a problem when trying to query my database.

I have three tables (don't think about names, their generalized for the purpose of this question) - CUSTOMER, CUSTORD, & ORDERS

CUSTOMER

  • Has a field called custID

ORDERS

  • Has a field called orderID

CUSTORD

  • Many to many relationship - One customer can have many orders and one order can have many customers
  • Contains...
    • custID (linked to CUSTOMER.custID)
    • orderID (linked to ORDERS.orderID)

Basically I need a way to put in a custID and get a list of all orders that a customer has and display that in a dataGridView.

What I've Tried

I've tried adding queries to the different tableAdapters using the Query Configuration Wizard in the DataSet Designer. The main problem with this is that when I try to use JOIN it gives me the error "JOIN expression not supported." JOIN is the only way that I know how to relate the data using SQL.

I've also tried adding the code manually but I can never get the information in the right format for being displayed in a dataGridView. If I can arrange all the orderID's that are related to the custID into an array maybe that is where I can find a solution. I can figure out how to loop through that info, querying according to each sibID, but how would I compile all that info into one tableAdapter to be displayed in the dataGridView?

Honestly anything helps and if you need some more info, just let me know.

Specifications

  • IDE: Visual Studio Ultimate 2012
  • Database created using: Microsoft Access 2010
  • Language: C#

Edit

So I went the route of putting everything into an array that I can parse later. still getting some errors but maybe it will shed some more light on what I'm trying to do. The error I'm getting is, "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign key constraints." I double checked and nothing was null and nothing is repeated. It might be the foreign key but how do I solve that? Here's the code...

  1. GetSelectedCustOrders takes in a custID and queries CUSTORD for matching orderID's
    • SELECT orderID FROM CUSTORD WHERE custID= selectedCust

DBDataSetTableAdapters.CUSTORDTableAdapter CUSTORDTableAdapter = new DBDataSetTableAdapters.CUSTORDTableAdapter();

    string relationString = "-1";
    DataTable relationResult = new DataTable();

    relationResult = CUSTORDTableAdapter.GetSelectedCustOrders(Convert.ToInt16(custID));

    foreach (DataRow row in relationResult.Rows)
    {
        foreach (DataColumn column in relationResult.Columns)
        {
            relationString += Convert.ToString(row[column]);
            testLabel.Text = relationString;
        }
    }

Edit 2

So the reason why I was getting the error above was because I was trying to change the schema of the table. If I change the SQL query statement to...

SELECT * FROM CUSTORD WHERE custID= selectedCust

...then it worked perfectly! I implemented an index into my foreach so I could select only the orderID and appended those onto the relationString. Now just to parse the info, convert it an int and run the query on ORDERS for the orderID's.

So I ended up deciding against running a second query. First I just filled my DataGridView with the whole ORDER table. After putting my orderID's into a comma delimited string, I split them into an array. I turned off all rows in my DataGridView and checked the orderID of the row against all the orderID's in my sRelationArray. If it matched, I turned the row back on. Code is as follows:

private void setOrderTable()
    {
        string relationString = getRelatedOrders(); //gives the comma delimited string
        string[] sRelationArray = relationString.Split(new[] {','}, System.StringSplitOptions.RemoveEmptyEntries);
        CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];

        try
        {
            this.oRDERSTableAdapter.Fill(this.DBDataSet.ORDERS);
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error finding orders. \n" + ex.Message);
        }

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            currencyManager1.SuspendBinding();
            row.Visible = false;

            for (int i = 0; i < sRelationArray.Length; i++)
            {
                if (sRelationArray[i] == row.Cells[0].Value.ToString())
                {
                    row.Visible = true;
                }
            }
        }
        currencyManager1.ResumeBinding();
    }//end set orders table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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