建网站详细步骤,个人可以做电影网站吗,wordpress图片批量修改,opensns wordpress一、安装
第一步#xff0c;在你的项目中找到解决方案#xff0c;右键-管理解决方案的Nuget 第二步#xff0c;下载对应的包#xff0c;注意你的框架是哪个就下载哪个的包#xff0c;一个项目安装一次包即可 点击应用和确定 安装好后会显示sqlsugar的包 二、使用#xf…一、安装
第一步在你的项目中找到解决方案右键-管理解决方案的Nuget 第二步下载对应的包注意你的框架是哪个就下载哪个的包一个项目安装一次包即可 点击应用和确定 安装好后会显示sqlsugar的包 二、使用增删改查
using SqlSugar;
using SqlSugar;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;[SugarTable(TestTable)] // 指定实体类对应的数据库表名
class TestTable
{[SugarColumn(IsPrimaryKey true)] // 指定主键列public string Id { get; set; }public string Name { get; set; }public int Age { get; set; }public string Content { get; set; }public int IsEnable { get; set; }public int IsDeleted { get; set; }public string Stage { get; set; }public string Remarks { get; set; }
}class Test
{public static int AddOrUpdateT(SqlSugarClient db,T entity) where T : class, new(){var entityIdProp GetEntityIdPropertyT();var entityIdValue entityIdProp.GetValue(entity);var dbEntity db.QueryableT().InSingle(entityIdValue);if (dbEntity ! null){// 根据 ID 查询到了记录执行更新操作return db.Updateable(entity).ExecuteCommand();}else{// 根据 ID 没有查询到记录执行插入操作return db.Insertable(entity).ExecuteCommand();}}public static PropertyInfo GetEntityIdPropertyT() where T : class, new(){var entityType typeof(T);var properties entityType.GetProperties();foreach (var property in properties){var attribute Attribute.GetCustomAttribute(property, typeof(SqlSugar.SugarColumn)) as SqlSugar.SugarColumn;if (attribute ! null attribute.IsPrimaryKey){return property;}}throw new Exception($实体类型 {entityType.FullName} 没有定义主键);}static void Main(string[] args){// 创建 SqlSugar 实例SqlSugarClient db new SqlSugarClient(new ConnectionConfig(){ConnectionString server DESKTOP-FTH2P3S; Database TestDb; Trusted_Connection SSPI;, // 数据库连接字符串DbType DbType.SqlServer, // 数据库类型IsAutoCloseConnection true, // 是否自动关闭数据库连接});// 1.插入数据var model new TestTable(){Id Guid.NewGuid().ToString(),Name Tom,Age 18,Content Hello World,IsEnable 1,IsDeleted 0,Stage Stage 1,Remarks Test};int insert_code db.Insertable(model).ExecuteCommand();//返回影响行数// 2.查询数据var list db.QueryableTestTable().ToList();// 3.自定义查询SQLvar result db.SqlQueryableTestTable(SELECT * FROM TestTable WHERE Age 30).ToList();// 4.更新数据var updateModel db.QueryableTestTable().Where(it it.Id 8ffd64fc-8aea-4641-a57b-d957ad0dd229).First();if (updateModel ! null){updateModel.Name Jerry;var update_code db.Updateable(updateModel).ExecuteCommand();//返回影响行数}// 5.删除数据var delete_code db.DeleteableTestTable().Where(it it.Id 8ffd64fc-8aea-4641-a57b-d957ad0dd229).ExecuteCommand();//返回影响行数//6.自主封装的方法有则添加无则插入根据主键ID匹配var updateModel2 new TestTable();updateModel2.Id 8ffd64fc-8aea-4641-a57b-d957ad0dd229;updateModel2.Name SuSu;int a AddOrUpdateTestTable(db, updateModel2);//返回影响行数}
}【备注】AddOrUpdate是自己写的方法。