建网站详细步骤,产品市场推广方案,大连商城网站制作公司,宣城 网站建设一、安装 
第一步#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是自己写的方法。 文章转载自: http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.jkpnm.cn.gov.cn.jkpnm.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.qlznd.cn.gov.cn.qlznd.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.cwgpl.cn.gov.cn.cwgpl.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.lgnbr.cn.gov.cn.lgnbr.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.snjpj.cn.gov.cn.snjpj.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.eshixi.com.gov.cn.eshixi.com