/// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="updateModel"></param>
        /// <param name="dao"></param>
        /// <param name="conn"></param>
        /// <param name="trns"></param>
        /// <returns></returns>
        public static int Update <T>(this T model,
                                     IDBUtil dao,
                                     T updateModel,
                                     DbConnection conn = null, DbTransaction trns = null) where T : IDBEntity
        {
            //處理 where 參數
            var whereParams = prepareWhereParams(model.GetOperating().WHERE_LIST);

            //處理 set 參數 (僅抓 model 中有異動到的參數)
            var setColumnsInfo = new Dictionary <string, object>();

            foreach (var pi in updateModel.GetType().GetProperties())
            {
                //var pName = "";
                ////忽略 db 欄位大小寫和 model 的不同
                //if (dic.ContainsKey(p.Name.ToUpper())) pName = p.Name.ToUpper();
                //if (dic.ContainsKey(p.Name.ToLower())) pName = p.Name.ToLower();

                if (updateModel.GetModeifyField().Contains(pi.Name))
                {
                    setColumnsInfo.Add(pi.Name, pi.GetValue(updateModel));
                }
            }

            return(dao.Update(model.GetTableName(), setColumnsInfo, whereParams, conn, trns));
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mdt">主表</param>
        /// <param name="mtablename">主表表名</param>
        /// <param name="ddt">子表</param>
        /// <param name="dtablename">子表表名</param>
        /// <returns></returns>
        public static int Update(DataTable mdt, string mtablename, DataTable ddt, string dtablename, bool isupdate = false)
        {
            int    retvalue = 0;
            string msql     = @"SELECT  FNo, FTitle
             FROM  " + mtablename + " WHERE FNo = '';";
            string dsql     = "select * from " + dtablename + " WHERE FMNo = ''";

            if (isupdate)
            {
                //   msql = @"SELECT  FNo, FTitle
                //FROM  " + mtablename + " WHERE FNo = '"+mdt.Rows[0]["FNo"].ToString()+"'";
                //   dsql = "select * from " + dtablename + " WHERE FMNo = '"+ddt.Rows[0]["FMNo"].ToString()+"'";
                for (int i = 0; i < mdt.Rows.Count; i++)
                {
                    mdt.Rows[i][0] = mdt.Rows[i][0];
                    mdt.AcceptChanges();
                    mdt.Rows[i].SetModified();
                }

                DataRowState rowState = ddt.Rows[0].RowState;
                for (int i = 0; i < ddt.Rows.Count; i++)
                {
                    ddt.Rows[i][1] = ddt.Rows[i][1];
                    ddt.AcceptChanges();
                    ddt.Rows[i].SetModified();
                }
                rowState = ddt.Rows[0].RowState;
            }
            try
            {
                dBUtil.OpenConn();
                dBUtil.BeginTrans();
                retvalue  = dBUtil.Update(mdt, msql);
                retvalue += dBUtil.Update(ddt, dsql);
                dBUtil.CommitTrans();
            }
            catch (Exception ex)
            {
                dBUtil.RollbackTrans();
            }
            finally
            {
                dBUtil.CloseConn();
            }
            return(retvalue);
        }