C#SQL封装(四)-创新互联

本篇接C# SQL封装(三)

在南湖等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、网站建设 网站设计制作按需设计网站,公司网站建设,企业网站建设,品牌网站制作,成都全网营销,成都外贸网站建设,南湖网站建设费用合理。

在其功能上继续扩展 改操作 , 让我们开始吧。。。。。

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

        ///          /// 更新一条数据的T-SQL构成(以id号来删除)         ///          ///          /// 原始model         /// 更改后的model         ///          public static string UPDATE_T_SQL(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)

        ///          /// 构建T-SQL参数 UPDATE         ///          ///          /// 原model         /// 更改后model         ///          public static SqlParameter[] UPDATE_T_SQL(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 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();                             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();                             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();                 }                 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(kayer_ori, kayer_change_copy);
           SqlParameter[] update_p = SqlParameter_Structure.UPDATE_T_SQL(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封装(四)

未完待续 ..........................

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:C#SQL封装(四)-创新互联
转载来源:http://pwwzsj.com/article/jdsjj.html