赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
如何安装使用SqlSugar和增删改查基本操作
一款轻量级并且特别强大的ORM,支持常见的关系型数据库。
①右键解决方案名称,点击管理Nuget程序包
搜索SqlSugar,选择安装版本,点击安装
选择版本时注意各个版本对.net环境的要求,一般选低一点 的就够用了
安装成功后可以看到引用中就多出了SqlSugar引用
该实例为对一个用户表进行增删改查操作
①创建一个用户表,结构如下
②创建好数据库表之后,调用下面SqlSugarUtil中的GenerateEntity()方法生成对应的Model类,或者自己手写创建也可以,用户model类Tb_User.cs如下:
- public partial class Tb_User
- {
- public Tb_User(){
-
- }
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public string F_UserId {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public string F_UaerName {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public string F_UserEmail {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public string F_Password {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public string F_UserPower {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public string F_UserArea {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public DateTime? F_RegisterTime {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public DateTime? F_InsertTime {get;set;}
-
- /// <summary>
- /// Desc:
- /// Default:NULL
- /// Nullable:True
- /// </summary>
- public DateTime? F_UpdateTime {get;set;}
-
- }
②以下为增删该查操作实例代码
- public class SqlSugarUtil
- {
-
- public static string ConnStr = @"Server=xxx.xx.xx.xxx;Initial Catalog=数据库名称;User ID=数据库用户ID;Password=密码;max pool size=512";
- /// <summary>
- /// 获取程序数据库操作对象
- /// </summary>
- /// <param name="strConn">数据库连接字符串</param>
- /// <returns></returns>
- public static SqlSugarClient GetDb(string strConn)
- {
- var db = new SqlSugarClient(
- new ConnectionConfig()
- {
- ConnectionString = strConn,
- DbType = DbType.SqlServer,
- IsAutoCloseConnection = true,
- InitKeyType = InitKeyType.Attribute,
- AopEvents = new AopEvents
- {
- OnLogExecuting = (sql, p) =>
- {
- Console.WriteLine(sql);
- Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
- }
- }
- });
- return db;
- }
-
-
- //根据连接字符串将数据库表生成实体
- public void GenerateEntity()
- {
- try
- {
- var db = GetDb(ConnStr);
- db.Ado.CheckConnection();
- var path = AppDomain.CurrentDomain.BaseDirectory + "\\Entity";//生成的实体存入的文件夹路径
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- db.DbFirst.CreateClassFile(path, "生成的文件的头部的解决方案名称");
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
-
-
-
- /// <summary>
- /// 查询所有的用户记录
- /// </summary>
- /// <returns></returns>
- public List<Tb_User> getUserList()
- {
- try
- {
- return AppDb.GetDb().Queryable<Tb_User>().ToList();
- }
- catch (Exception ex)
- {
- return null;
- }
- }
-
-
-
- /// <summary>
- /// 查询符合条件的第一条记录
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public Tb_User getUser(Tb_User model)
- {
- try
- {
- return AppDb.GetDb().Queryable<Tb_User>().Where(it => (it.F_UserEmail.Equals(model.F_UserEmail))).First();
- }
- catch (Exception ex)
- {
- return null;
- }
- }
-
-
- /// <summary>
- /// 插入一条学生记录
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public int InsertUser(Tb_User model)
- {
- try
- {
- return AppDb.GetDb().Insertable<Tb_User>(model).ExecuteCommand();
- }
- catch (Exception ex)
- {
- return -1;
- }
- }
-
-
- /// <summary>
- /// 查询之前对所给参数进行判断,然后查询出所有符合条件的记录(可对结果按某个字段进行排序)
- /// </summary>
- /// <param name="selectModel"></param>
- /// <returns></returns>
- public List<Tb_User> getUserList(Tb_User selectModel)
- {
- try
- {
- return AppDb.GetDb().Queryable<Tb_User>()
- .WhereIF(selectModel.F_UaerName != null && selectModel.F_UaerName.Trim() != "", it => it.F_UaerName.Equals(selectModel.F_UaerName))
- .WhereIF(selectModel.F_UserEmail != null && selectModel.F_UserEmail.Trim() != "", it => it.F_UserEmail.Equals(selectModel.F_UserEmail))
- .OrderBy("F_UaerName DESC")//按照用户名倒序排序
- .ToList();
- }
- catch (Exception ex)
- {
- return null;
- }
- }
-
-
- /// <summary>
- /// 更新符合条件的用户记录的用户名
- /// </summary>
- /// <param name="model"></param>
- /// <param name="newName"></param>
- /// <returns></returns>
- public int updRecord(Tb_User model, string newName)
- {
- int resultCount = 0;
- try
- {
- resultCount = AppDb.GetDb().Updateable<Tb_User>()
- .Where(it => it.F_UaerName == model.F_UaerName &&
- it.F_UserEmail == model.F_UserEmail)
- .SetColumns(p => p.F_UaerName == model.F_UaerName)
- .ExecuteCommand();
- }
- catch (Exception e)
- {
- return -1;
- }
- return resultCount;
- }
-
-
-
-
- }
还有其他操作,比如执行自定义sql语句,后续更新
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。