提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
如何安装使用SqlSugar和增删改查基本操作
一款轻量级并且特别强大的ORM,支持常见的关系型数据库。
①右键解决方案名称,点击管理Nuget程序包

搜索SqlSugar,选择安装版本,点击安装

选择版本时注意各个版本对.net环境的要求,一般选低一点 的就够用了

安装成功后可以看到引用中就多出了SqlSugar引用

- public class SqlSugarUtil
- {
-
- public static string ConnStr = @"Server=xxx.xx.xx.xxx;Initial Catalog=数据库名称;User ID=数据库用户ID;Password=密码;max pool size=512";
- ///
- /// 获取程序数据库操作对象
- ///
- /// 数据库连接字符串
- ///
- 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);
- }
- }
-
-
-
- ///
- /// 查询所有的学生记录
- ///
- ///
List 类型集合 - public List<Tb_Student> getUpdRecordList()
- {
- try
- {
- return GetDb(ConnStr).Queryable
().ToList(); - }
- catch (Exception ex)
- {
- return null;
- }
- }
-
-
-
-
-
-
-
- }