none
how to use Data Access Block 2.0 to update hierarchical changes RRS feed

  • Question

  • I read some rows from the Orders & Order Details table in the Northwind database, then make some changes, including adding & deleting. When i summit the dataset to the database, i should submit the changes in the following order:
    1. Submit new customers.

    2. Submit new orders.

    3. Submit deleted orders.

    4. Submit deleted customers.

    But the Database class in Data Access Block 2.0 has no method to update an array of DataRow.

    Is there a solution for this task using Data Access Block 2.0?

     

    Thank you in advance.

    Saturday, December 29, 2007 8:44 AM

All replies

  • hi,

    I don't quite know what you want to do. I just guess you need transaction to complete the serial operation. And the following code shows you how to implete transaction with ADO.net. Hope it can help you.

    Code Snippet

    public void ProcessCreditRisk(bool throwEx, int custID)
    {
    // First, look up current name based on customer ID.
    string fName = string.Empty;
    string lName = string.Empty;
    SqlCommand cmdSelect = new SqlCommand(
    string.Format("Select * from Customers where CustID = {0}", custID), sqlCn);
    using (SqlDataReader dr = cmdSelect.ExecuteReader())
    {
    while (dr.Read())
    {
    fName = (string)dr["FirstName"];
    lName = (string)dr["LastName"];
    }
    }
    // Create command objects that represent each step of the operation.
    SqlCommand cmdRemove = new SqlCommand(
    string.Format("Delete from Customers where CustID = {0}", custID), sqlCn);
    SqlCommand cmdInsert = new SqlCommand(string.Format("Insert Into CreditRisks" +
    "(CustID, FirstName, LastName) Values" +
    "({0}, '{1}', '{2}')", custID, fName, lName), sqlCn);
    // We will get this from the connection object.
    SqlTransaction tx = null;
    try
    {
    tx = sqlCn.BeginTransaction();
    // Enlist the commands into this transaction.
    cmdInsert.Transaction = tx;
    cmdRemove.Transaction = tx;
    // Execute the commands.
    cmdInsert.ExecuteNonQuery();
    cmdRemove.ExecuteNonQuery();
    // Simulate error.
    if (throwEx)
    {
    throw new ApplicationException("Sorry! Database error! Tx failed...");
    }

    // Commit it!
    tx.Commit();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    // Any error will roll back transaction.
    tx.Rollback();
    }
    }

     

     

    Regards

    Jing

    Thursday, June 26, 2008 10:40 AM