none
How to insert relational typed dataset into DataContext ? RRS feed

  • Question

  • I have a relational typed dataset that has 2 tables in it:

    1) Command (PK ID, FK log_ID, ...) 
    2) Log (PK ID)

    The Command table has a foreign key to the Log table.

    I also have a DataContext (with the same structure) that communicates with my MSSQL CE database. How can I insert the data in the dataset into the datacontext so that relations between Log and Command wouldnt dissappear ?


    Best Wishes,
    D
    Thursday, August 13, 2009 6:45 AM

Answers

  • Hi desmond5,

    If you generated your dataset and datacontext based on the database you were dealing with, the relations between Log and Command should be there. In other words, if your insertion into the datacontext violates the consistency rule you set with PK and FK, error would occur in your program.

    Here's a simple example for your case. Remember that you should add the Log changes first, then the Command.

                DataClasses1DataContext db = new DataClasses1DataContext(@"...");
                foreach (DataRow row in ds.Tables["log"].Rows)
                {
                      log log1 = new log {
                       // logID = ds.Tables["log"].Rows[0].Field<int>("logID"),
                      //logString = ds.Tables["log"].Rows[0].Field<string>("logString")
                      logID = row.Field<int>("logID"),
                      logString = row.Field<string>("logString")
                       };
                      db.log.InsertOnSubmit(log1);
                }

                foreach (DataRow row in ds.Tables["command"].Rows)
                {
                    command command1 = new command
                    {
                        logID = row.Field<int>("logID"),
                        commandString = row.Field<string>("commandString"),
                        commandID = row.Field<int>("commandID")
                    };
                    db.command.InsertOnSubmit(command1);
                }
                db.SubmitChanges();


    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, August 14, 2009 9:12 AM

All replies

  • Hi desmond5,

    If you generated your dataset and datacontext based on the database you were dealing with, the relations between Log and Command should be there. In other words, if your insertion into the datacontext violates the consistency rule you set with PK and FK, error would occur in your program.

    Here's a simple example for your case. Remember that you should add the Log changes first, then the Command.

                DataClasses1DataContext db = new DataClasses1DataContext(@"...");
                foreach (DataRow row in ds.Tables["log"].Rows)
                {
                      log log1 = new log {
                       // logID = ds.Tables["log"].Rows[0].Field<int>("logID"),
                      //logString = ds.Tables["log"].Rows[0].Field<string>("logString")
                      logID = row.Field<int>("logID"),
                      logString = row.Field<string>("logString")
                       };
                      db.log.InsertOnSubmit(log1);
                }

                foreach (DataRow row in ds.Tables["command"].Rows)
                {
                    command command1 = new command
                    {
                        logID = row.Field<int>("logID"),
                        commandString = row.Field<string>("commandString"),
                        commandID = row.Field<int>("commandID")
                    };
                    db.command.InsertOnSubmit(command1);
                }
                db.SubmitChanges();


    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, August 14, 2009 9:12 AM
  • Hi desmond5,

    Do you have the two tables in your database?

    I'm just curious why do you use dataset and datacontext in the same time. Because each of them can inserting records into database.

    Could you provide more details about your problem?

    Best Regards

    Yichun Feng

     

    Sunday, August 16, 2009 6:03 AM