2011-12-21 6 views
0

DataSetを非常にうまく処理する方法がわかりません(VBのRecordSetで動作するようになっていました)。 DataSetを使用する正しい方法は何ですか?私が解決したいDataSet(複数のテーブルでのInsert/Update)

問題:

  • データアダプター、データセットの2つのインスタンス、本当に必要な使用ですか..?
  • 変更をコミットする正しい方法(更新時ではなく行を追加するときの作業)
  • finallyブロックにはどのような処理をする必要がありますか?

ありがとうございました。

いくつかのループと一貫性があるのでコードは長いですが、混乱させるのは、DataSetを使用して行を編集または挿入することです。

public bool SaveData() 
{ 
    bool resp = false; 
    this.pObs = null; 
    bool editing = false; 

    //StringBuilder stringBuilder;  
    string sqlQuery = "SELECT * FROM BooksTemp"; 
    string sqlQuery2 = "SELECT * FROM Categories;";  

    using (SqlConnection sqlConnection = new SqlConnection(connectionString)) 
    { 
     SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, connectionString); 
     SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); 

     SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter(sqlQuery2, connectionString); 
     SqlCommandBuilder sqlCommandBuilder2 = new SqlCommandBuilder(sqlDataAdapter2); 
     DataSet dataSet2 = new DataSet(); 

     DataSet dataSet = new DataSet(); 
     DataTable dataTable = null; 
     DataRow Row = null; 

     try 
     { 
      sqlDataAdapter.Fill(dataSet, "BooksTemp"); 
      dataTable = dataSet.Tables["BooksTemp"]; 

      sqlDataAdapter2.Fill(dataSet2, "Categories"); 
      DataTable tableCategorias = dataSet2.Tables["Categories"]; 

      int i = 0; 
      foreach (Ebook ebook in pListEbooks) 
      { 
       editing = false; 

       #region loop_categories 

       /* Loop all book categories before save in temporary book table*/ 
       string codCategorias = null; 
       if (ebook.categories != null) 
       { 
        bool aux; 
        string catID; 

        try 
        { 
         foreach (Categorias categoria in ebook.categories) 
         { 
          aux = false; 
          /* Search the categorie in DB */ 
          if(tableCategorias.Select("CDD = '" + categoria.code + "'").Length > 0) 
           aux = true; 

          /* Include categorie in DB */ 
          if (!aux) 
          { 
           /* Generate an ID */ 
           catID = Strings.Codify(); 

           //tableCategorias.Rows.Find(catID) didnt work 
           while (tableCategorias.Select("ID = '" + catID + "'").Length > 0) 
           { 
            catID = Strings.Codifica(); 
           } 
           Row = tableCategorias.NewRow(); 
           Row.BeginEdit(); 

           Row["ID"] = catID; 
           Row["Nome"] = categoria.description; 

           tableCategorias.Rows.Add(Row); 
           sqlDataAdapter2.Update(tableCategorias); 
          } 
         } 
        } 
        catch { } 
        finally 
        { 
         // Shoud I dispose or close something here? 
        } 
       } 

       #endregion 

       /* Verify if the book already have changes in DB */     
       if (dataTable.Select("DistribuidorLivroID = '" + ebook.id + "'").Length == 1) 
       { 
        /* Edit row with new ebook changes */ 
        editing = true; 
        Row = dataTable.Rows[i]; 
        Row.BeginEdit(); 
        Row["UpdatedOn"] = DateTime.Now; 
       } 
       else 
       { 
        /* Add new row with ebook changes */ 
        Row = dataTable.NewRow(); 
        Row.BeginEdit(); 
        Row["CreatedOn"] = DateTime.Now; 
       } 
       Row["DistribuidorLivroID"] = ebook.id; 

       if (ebook.price != null) 
        Row["Price"] = ebook.price; 
       if (ebook.sorting_title != null) 
        Row["Title"] = ebook.title; 
       if (ebook.cover_image != null) 
        Row["Foto"] = ebook.cover_image; 

       if (!editing) 
        dataTable.Rows.Add(Row); 
       // else 
       //  Row.AcceptChanges(); 

       // Commiting only when I add new row and not when I edit a row 
       sqlDataAdapter.Update(dataTable); 
       i++; 
      } 
     } 
     catch { } 
     finally 
     { 
      // What should I dispose here?    
     } 
    } 
    return resp; 
} 
+0

構文の強調表示はコードの前に4スペースです(ほとんどのコピーはビルダーからコピーされますので、自動的にスペースが入ります) – Moonlight

+0

これは私の目を痛めます。 – ThePower

+0

私はコードの無関係な部分を削除します。 –

答えて

0

入力したデータセットを使用することをおすすめします。これらはすべての問題を解決し、コードの品質を向上させます。

+0

私はDataSetを使用しているような親切な人だと言っていたので、正しい構造と使用するための正しい方法を理解できませんでした。 (私は本当にそれについて研究したので、私はここでなぜ尋ねているのですか)。詳細は参考になります。 –