中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C# SQL封裝(四)

發布時間:2020-07-03 11:18:08 來源:網絡 閱讀:522 作者:Aonaufly 欄目:數據庫

本篇接C# SQL封裝(三)

在其功能上繼續擴展 改操作 , 讓我們開始吧。。。。。

一 : 構建T-SQL語句(在SQL_Structure.cs中添加一個方法 UPDATE_T_SQL , 用于構建update的T-SQL)

        /// <summary>
        /// 更新一條數據的T-SQL構成(以id號來刪除)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model">原始model</param>
        /// <param name="model_change">更改后的model</param>
        /// <returns></returns>
        public static string UPDATE_T_SQL<T>(T model , T model_change) where T : BaseModel
        {
            Type model_type = model.GetType();
            Type model_change_type = model_change.GetType();
            if (model_type.Name == model_change_type.Name)
            {
                string base_update = @"update [{0}] set {1} where {2} = @{3}";
                string[] arr = model_type.Name.Split(new char[] { '.' });
                string table_name = arr[arr.Length - 1];//獲得表的名稱
                PropertyInfo[] p_intos = model_type.GetProperties();
                PropertyInfo item = null;
                PropertyInfo item_change = null;
                string item_value = string.Empty;
                string item_change_value = string.Empty;
                StringBuilder update_set = null;
                for (int i = 0; i < p_intos.Length; i++)
                {
                    item = p_intos[i];
                    item_change = model_change_type.GetProperty(item.Name);
                    item_value = item.GetValue(model, null).ToString();
                    item_change_value = item_change.GetValue(model_change, null).ToString();
                    if (item.Name == "id")
                    {
                        //比較id值是否一致
                        if (item_value != item_change_value)
                        {
                            throw new Exception(
                                string.Format("model類 id : {0} 與model_change類id:{1} 不一致,請確保是同一條數據",
                                    item_value,
                                    item_change_value
                                )
                                );
                        }
                    }
                    else
                    {
                        if (item_value != item_change_value)
                        {
                            if (update_set == null) update_set = new StringBuilder();
                            update_set.Append(
                                string.Format( @" {0} = @{1}," , item.Name , item.Name )
                                );
                        }
                    }
                }
                if (update_set != null)
                {
                    return string.Format( base_update,
                        table_name,
                        update_set.ToString().Substring(0, update_set.ToString().Length - 1),
                        "id",
                        "id"
                        );
                }
                else
                {
                    return string.Empty;//不存在更新
                }
            }
            else
            {
                throw new Exception(
                    string.Format("model類 : {0} 與model_change類 :{1} 不匹配",
                        model_type.Name ,
                        model_change_type.Name
                        )
                    );
            }
        }

二 : 構建SqlParameter參數 (在SqlParameter_Structure.as中添加方法UPDATE_T_SQL

        /// <summary>
        /// 構建T-SQL參數 UPDATE
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model">原model</param>
        /// <param name="model_change">更改后model</param>
        /// <returns></returns>
        public static SqlParameter[] UPDATE_T_SQL<T>(T model, T model_change) where T : BaseModel
        {
            Type model_type = model.GetType();
            Type model_change_type = model_change.GetType();
            if (model_type.Name == model_change_type.Name)
            {
                PropertyInfo[] p_intos = model_type.GetProperties();
                PropertyInfo item = null;
                PropertyInfo item_change = null;
                string item_value = string.Empty;
                string item_change_value = string.Empty;
                List<SqlParameter> sql_param = null;
                SqlParameter cell = null;
                bool is_change_model = false;//是否有字段進行了更改
                for (int i = 0; i < p_intos.Length; i++)
                {
                    item = p_intos[i];
                    item_change = model_change_type.GetProperty(item.Name);
                    item_value = item.GetValue(model, null).ToString();
                    item_change_value = item_change.GetValue(model_change, null).ToString();
                    if (item.Name == "id")
                    {
                        if (item_value != item_change_value)
                        {
                            throw new Exception(
                                string.Format("model類 id : {0} 與model_change類id:{1} 不一致,請確保是同一條數據",
                                    item_value,
                                    item_change_value
                                )
                                );
                        }
                        else
                        {
                            if (sql_param == null) sql_param = new List<SqlParameter>();
                            cell = new SqlParameter(item.Name, SqlParameter_Structure.Get_SqlDbType_SqlType(item.PropertyType));
                            cell.Value = item.GetValue(model, null);
                            sql_param.Add(cell);
                        }
                    }
                    else
                    {
                        if (item_value != item_change_value)
                        {
                            if (!is_change_model) is_change_model = true;
                            if (sql_param == null) sql_param = new List<SqlParameter>();
                            cell = new SqlParameter(item_change.Name, SqlParameter_Structure.Get_SqlDbType_SqlType(item_change.PropertyType));
                            cell.Value = item_change.GetValue(model_change, null);//加入改變的值
                            sql_param.Add(cell);
                        }
                    }
                }
                if (is_change_model)
                {
                    return sql_param.ToArray<SqlParameter>();
                }
                else
                {
                    return null;//沒有任何字段更改
                }
            }
            else
            {
                throw new Exception(
                    string.Format("model類 : {0} 與model_change類 :{1} 不匹配",
                        model_type.Name,
                        model_change_type.Name
                        )
                    );
            }
        }

測試:

C# SQL封裝(四)

測試代碼


            //------------------------模擬一條重數據庫得到的數據 begin ---------------------------------------
            User kayer_ori = new User();
            kayer_ori.id = 3;
            kayer_ori.name = "kayer";
            kayer_ori.sex = 1;
            kayer_ori.lv = 1;
            kayer_ori.username = "Kayer";
            kayer_ori.userpwd = "123";
            //------------------------模擬一條重數據庫得到的數據 end -----------------------------------------
            //拷貝一條kayer_ori數據 ( 可以用原型模式 , 這里我直接使用笨辦法 )
            User kayer_change_copy = new User();
            kayer_change_copy.id = kayer_ori.id;
            kayer_change_copy.name = kayer_ori.name;
            kayer_change_copy.sex = kayer_ori.sex;
            kayer_change_copy.lv = kayer_ori.lv;
            kayer_change_copy.username = kayer_ori.username;
            kayer_change_copy.userpwd = kayer_ori.userpwd;
            // --- 改變數據 ---
            kayer_change_copy.name = "Aonaufly";
            kayer_change_copy.lv = 4;
            string sql_update = SQL_Structure.UPDATE_T_SQL<User>(kayer_ori, kayer_change_copy);
            SqlParameter[] update_p = SqlParameter_Structure.UPDATE_T_SQL<User>(kayer_ori, kayer_change_copy);
            int i = SqlHelper.ExecteNonQuery(CommandType.Text, sql_update, update_p);
            if (i > 0)
            {
                Console.WriteLine("執行操作成功");
            }
            else
            {
                Console.WriteLine("執行操作失敗");
            }


結果 :

C# SQL封裝(四)

在數據庫中:

C# SQL封裝(四)


未完待續 ..........................


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

化州市| 柏乡县| 区。| 忻州市| 延寿县| 梨树县| 新津县| 田林县| 黔西县| 修武县| 平定县| 定日县| 清流县| 临城县| 通道| 丹巴县| 太仆寺旗| 巴塘县| 宜兰市| 泰州市| 利川市| 丰宁| 保亭| 元氏县| 张北县| 双鸭山市| 平昌县| 恩施市| 商南县| 满城县| 花莲县| 咸丰县| 庐江县| 武乡县| 图木舒克市| 阳江市| 济宁市| 陆良县| 虎林市| 隆子县| 锦州市|