开发者

How to move to next row in datatable if one row catches an error? C# 2.0

I am getting data from a mySql database and I am inserting it into another system. If a column has data in an incorrect format I log this and go to next row in the datatable. It works as expected but now if I have a search function in my method that gets some additional data and this function fails I want to immediately log this and go to next row. As it is now I just log it but it still gets inserted (without the value that didn't meet the search criteria).

My code:

    private void updateCustomer()
            {
                 MySqlConnection connection = new MySqlConnection("server=myServer;database=myDatabase;uid=myID;password=myPass");

                MySqlCommand command = new MySqlCommand(@"mySelectCommand", connection);
                DataTable customerTbl = new DataTable();
                MySqlDataReader reader;
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        customerTbl.Load(reader);
                    }
                    reader.Close();

                }
                catch (Exception ex)
                {
            _out.error("Could not connect to mySql database");
                }
                finally
                {
                    connection.Close();
                }

                foreach (DataRow row in customerTbl.Rows)
                {
                    // Declare the customer variables
                    string customerID = Encoding.ASCII.GetString((byte[])row["Customer ID"]);
                    string ChildOf = row["Child of"].ToString();

                    // Create the customer object
                    Customer customer = new Customer();

            customer.entityId = customerID;


                    if (ChildOf != "")
                    {
            RecordRef parentRef = new RecordRef();
                        try
                        {

                            parentRef.internalId = searchCustomer(ChildOf);

                        }
                        catch
                        {
// If it fails here I want to log the customerID and then go to the next row in the datatable (could not find the internalid for ChildOf
                   开发者_运维知识库         _out.error(customerID + " was not updated. Error: invalid format Parent string");
                        }
                finally
                {
                parentRef.typeSpecified = false;
                            customer.parent = parentRef;
                }
                    }



                    // Invoke update() operation
                    WriteResponse response = _service.update(customer);

                    // Process the response
                    if (response.status.isSuccess)
                    {

                    }
                    else
                    {
                        _out.error(customerID + " was not updated. Error: " + getStatusDetails(response.status));
                    }
                }
            }


You need to remove the row in the catch block, and change the foreach loop to a backwards for loop to handle the removals.


I realized that I want to log the other failed fields as well. Maybe it's an inefficient way but I did something like:

        bool findParent = true;
        if (ChildOf != "")
        {
            try
            {
                RecordRef parentRef = new RecordRef();
                parentRef.internalId = searchCustomer(ChildOf);
                parentRef.typeSpecified = false;
                customer.parent = parentRef;
            }
            catch
            {
                findParent = false;
                _out.error(customerID + " was not inserted. Error: invalid format Parent string");
            }
        }

And then an if statement before trying to insert:

if (findPartner == true && findParent == true)
                {
                    response = _service.add(customer);
                    // Process the response
                    if (response.status.isSuccess)
                    {

                    }
                    else
                    {
                        _out.error(customerID + " was not inserted. Error: " + getStatusDetails(response.status));
                    }

                }
                else
                {
                    //_out.error(customerID + " was not updated. Error: " + getStatusDetails(response.status));
                }


Use the row.HasError property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜