1. 程式人生 > >SqlDataAdapter.update結合事務(SqlTransaction)批量更新資料

SqlDataAdapter.update結合事務(SqlTransaction)批量更新資料

 //此處的程式功能主要是將一個庫中的表資料(源表)更新到另外一個庫中的表資料(目標表)

//實現目標表的資料和源表資料一致,已包括增加,更新和刪除資料記錄功能

//主呼叫程式碼塊

SqlConnection conn = new SqlConnection("data source=.;user id=sa;password=sa;initial catalog=test");
                    SqlTransaction trans;
                    conn.Open();
                    trans = conn.BeginTransaction();
                    try
                    {

                        TransUpdateTable(ds1.Tables[0],dtto, conn, sql1, trans, "key1");
                        TransUpdateTable(ds2.Tables[0],dtto, conn, sql2, trans, "key2");     

                        TransUpdateTable(ds3.Tables[0], dtto,conn, sql3, trans, "key3");
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }    

  /// <summary>
        /// 處理資料
        /// </summary>
        /// <param name="dtfrom">源資料表</param>
        /// <param name="conn">連線串</param>
        /// <param name="sql">要更新表的查詢語句</param>
        /// <param name="trans">事務</param>
        public void TransUpdateTable(DataTable dtfrom,DataTable dtto,SqlConnection conn, string sql, SqlTransaction trans, string key)
        {
            //
            try
            {
                SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
                da1.UpdateCommand = new SqlCommand("", conn, trans);
                da1.InsertCommand = new SqlCommand("", conn, trans);
                da1.DeleteCommand = new SqlCommand("", conn, trans);
                da1.SelectCommand = new SqlCommand(sql, conn, trans);
                //操作要更新的TO物件表
                UpdateDataTableByTableKey(key, dtfrom, dtto);

                SqlCommandBuilder sqlcmd = new SqlCommandBuilder(da1);
                da1.UpdateCommand = sqlcmd.GetUpdateCommand();
                da1.InsertCommand = sqlcmd.GetInsertCommand();
                da1.DeleteCommand = sqlcmd.GetDeleteCommand();
                da1.Update(dtto);
                dtto.AcceptChanges();
            }
            catch { throw; }

        }        

 /// <summary>
        /// 更新datatable
        /// </summary>
        /// <param name="key">表關鍵字</param>
        /// <param name="dtfrom">源表</param>
        /// <param name="dtto">目標表(要更新的表)</param>

public void UpdateDataTableByTableKey(string key, DataTable dtfrom,DataTable dtto)
        {
            bool flag = false;
            int row = -1;
            for (int i = 0; i < dtfrom.Rows.Count; i++)
            {
                //判斷是否存在記錄
                for (int j = 0; j < dtto.Rows.Count; j++)
                {
                    if (dtfrom.Rows[i][key].ToString() == dtto.Rows[j][key].ToString())
                    {
                        flag = true;//標誌存在記錄
                        row = j;//記錄存在行號
                        break;
                    }
                }
                if (flag && row >= 0)
                {
                    //迴圈列
                    for (int m = 0; m < dtto.Columns.Count; m++)
                    {
                        //存在列則更新
                        string columnname = dtto.Columns[m].ColumnName;
                        if (dtfrom.Columns.Contains(columnname))
                        {
                            //
                            dtto.Rows[row][columnname] = dtfrom.Rows[i][columnname];
                        }
                        else
                        {
                            dtto.Rows[i][columnname] = null;
                        }
                    }
                }
                else
                {
                    //不存在記錄則新增
                    DataRow dr = dtto.NewRow();
                    //迴圈列
                    for (int m = 0; m < dtto.Columns.Count; m++)
                    {
                        //存在列則更新
                        string columnname = dtto.Columns[m].ColumnName;
                        if (dtfrom.Columns.Contains(columnname))
                        {
                            //
                            dr[columnname] = dtfrom.Rows[i][columnname];
                        }
                        else
                        {
                            dr[columnname] = null;
                        }
                    }
                    dtto.Rows.Add(dr);
                }
                flag = false;
                row = -1;
            }
            //判斷刪除記錄
            for (int m = 0; m < dtto.Rows.Count; m++)
            {
                for (int n = 0; n < dtfrom.Rows.Count; n++)
                {
                    if (dtfrom.Rows[n][key].ToString() == dtto.Rows[m][key].ToString())
                    {
                        flag = true;//標誌存在記錄
                        row = m;//記錄存在行號
                        break;
                    }
                }
                if (!flag && row < 0)
                {
                    dtto.Rows[m].Delete();                    
                }
                flag = false;
                row = -1;
            }
          

        }