locked
DataRelation Many-to-Many RRS feed

  • Question

  • hello. A question please. When I create a One-to-many relation with a datarelation, it works fine. My problem is creating a many-to-many. Could anyone help me?

    Thanks....

    Thursday, January 25, 2007 6:43 PM

Answers

  • Hi

    Can you confirm the exact nature of the problem you are experiencing and perhaps post the code you use to create the data relation?

    Remember that a many to many is simply a relationship between two fields where neither one contains unique values.

    Richard

    Friday, January 26, 2007 11:38 AM

All replies

  • Hi

    Can you confirm the exact nature of the problem you are experiencing and perhaps post the code you use to create the data relation?

    Remember that a many to many is simply a relationship between two fields where neither one contains unique values.

    Richard

    Friday, January 26, 2007 11:38 AM
  • Here's my code. I have a many-to-many relationship between books and authors but don't know how to set up the DataRelation.

    // Create DataAdapter object for update and other operations
    SqlDataAdapter thisAdapter = new SqlDataAdapter(
    "SELECT pkey, title FROM books WHERE book = 1", thisConnection);

    //Create CommandBuilder object to build SQL commands
    SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);

    //Create DataSet to contain related data tables, rows and columns
    DataSet thisDataSet = new DataSet();

    //Set up DataAdapter objects for each table and fill
    SqlDataAdapter bookAdapter = new SqlDataAdapter(
    "
    SELECT * FROM BOOKS", thisConnection);

    SqlDataAdapter authorAdapter = new SqlDataAdapter(
    "SELECT * FROM Authors", thisConnection);

    SqlDataAdapter bookAuthAdapter = new SqlDataAdapter(
    "SELECT * FROM bkauthlink", thisConnection);

    bookAdapter.Fill(thisDataSet, "Books");
    authorAdapter.Fill(thisDataSet,
    "Authors");
    bookAuthAdapter.Fill(thisDataSet,
    "bkauthlink");

    //Set up DataRelation between books and authors
    DataRelation bookBkAuthLinkRel = thisDataSet.Relations.Add("bookBkAuthLink",
    thisDataSet.Tables[
    "Books"].Columns["pkey"],thisDataSet.Tables["bkauthlink"].Columns["bookskey"];

    Wednesday, February 25, 2009 6:19 PM