【问题标题】:How to move to next row in datatable if one row catches an error? C# 2.0如果一行出现错误,如何移动到数据表中的下一行? C# 2.0
【发布时间】:2010-11-29 01:41:30
【问题描述】:

我正在从 mySql 数据库中获取数据,并将其插入到另一个系统中。如果一列的数据格式不正确,我会记录下来并转到数据表中的下一行。它按预期工作,但现在如果我的方法中有一个搜索功能可以获取一些额外的数据并且该功能失败,我想立即记录并转到下一行。现在我只是记录它,但它仍然被插入(没有不符合搜索条件的值)。

我的代码:

    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));
                    }
                }
            }

【问题讨论】:

    标签: c# mysql database


    【解决方案1】:

    您需要删除 catch 块中的行,并将 foreach 循环更改为向后的 for 循环以处理删除。

    【讨论】:

      【解决方案2】:

      我意识到我也想记录其他失败的字段。也许这是一种低效的方式,但我做了类似的事情:

              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");
                  }
              }
      

      然后是一个 if 语句,然后再尝试插入:

      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));
                      }
      

      【讨论】:

        【解决方案3】:

        使用row.HasError 属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-08
          • 2012-03-23
          • 1970-01-01
          • 2019-05-30
          • 2012-03-23
          • 1970-01-01
          • 2017-07-09
          • 2015-12-09
          相关资源
          最近更新 更多