2010-11-29 7 views
0

私はmySqlデータベースからデータを取得しており、別のシステムに挿入しています。列のデータが不正な形式である場合は、これを記録し、データテーブル内の次の行に移動します。それは期待どおりに動作しますが、私はいくつかの追加データを取得し、この関数が失敗する私のメソッドで検索機能を持っている場合、私はすぐにこれを記録し、次の行に移動したい。今のように私はただログを記録しますが、まだ挿入されます(検索条件に合わない値はありません)。ある行がエラーをキャッチすると、データテーブルの次の行に移動するにはどうすればよいですか? C#2.0

マイコード:

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

答えて

0

あなたはcatchブロック内の行を削除し、削除を処理するために後方forループにforeachループを変更する必要があります。

0

私は、他の失敗したフィールドも同様にログに記録したいと思いました。多分それは非効率的な方法ですが、私は次のように何かをした:

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

row.HasErrorプロパティを使用します。

関連する問題