簡體   English   中英

C#-數據集(/在多個表中插入/更新)

[英]C# - DataSet (/Insert/Update in Multiple tables)

我不知道如何很好地處理DataSet(用於在VB中與RecordSet一起使用),所以我做了一個看起來很亂的方法。 想知道哪種是使用DataSet的正確方法。

我想解決的問題:

  • 真的有必要使用DataAdapter,DataSet ..的2個實例嗎?
  • 提交更改的正確方法(添加行時有效,更新時不行)
  • 我應該在final塊中放置什么?

感謝您的關注。

代碼很長,因為有一些循環和一致性,但令我感到困惑的是使用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;
}

我建議您使用類型化的數據集。 這些將解決您的所有問題並提高代碼質量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM