2023年7月13日发(作者:)
SqlSugar操作笔记创建数据表特性1.实体类实例 class Student { ///
主键ID /// [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] public int id { get; set; } public string Name { get; set; } public int Age { get; set; } }ar实⽤类 ///
使⽤该类时需要在包管理器中
安装:sqlSugarCore ///
学习连接:/Doc/1/1181 /// class Services class Services { private static Services _instance = null; public static Services GetInstance => _instance ?? (_instance = new Services()); private SqlSugarClient db; public SqlSugarClient Db => db; ///
在构造函数中连接数据库 /// private Services() { //连接本地数据库(服务器名称) string sqlConn = "Data Source=.;"; //数据库名称 sqlConn += "Initial Catalog = TestTable;"; //连接成功后是否保存密码信息",True表⽰保存,False表⽰不保存 sqlConn += "Persist Security Info = True;"; //⽤户名/密码 sqlConn += "User ID = sa;Password = Aa123456;"; //min pool size :连接池最⼩尺⼨,⾸次连接池创建连接数量,max pool size:连接池最⼤尺⼨ sqlConn += "Max Pool Size = 512;"; db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = sqlConn,//连接符字串 DbType = ver,//该项报发⽣冲突错误时,移除命名空间引⼊的冲突对象 IsAutoCloseConnection = true }); } ///
⽣成实体类 /// /// 表名,⽣成实体类的名称 /// 实体类⽣成路径 /// 实体类命名空间 public void MCreateClassFile(string objectNames, string path, string strNameSpace) { //⽣成
当前连接数据库所有数据表
的实体类 ClassFile(path, strNameSpace); //⽣成
数据表名为objectNames参数值的数据表
的实体类 (objectNames).CreateClassFile(path, strNameSpace); //⽣成
数据表名转换为⼩写后
以t开头的数据表
的实体类 (it => r().StartsWith("t")).CreateClassFile(path, strNameSpace); //⽣成
数据表名为objectNames参数值的数据表
的实体类并包含特性 (objectNames).IsCreateAttribute().CreateClassFile(path, strNameSpace); //⽣成
数据表名为objectNames参数值的数据表
的实体类并包含默认值 teDefaultValue().CreateClassFile(path, strNameSpace); } ///
创建数据表 /// public void CreatTable() { //Student
为实体类类名 //创建GUID主键的数据表时,主键类型设置为Guid即可,⽆需添加特性,数据库中id类型表⽰为:uniqueidentifier bles(typeof(Student)); bles(typeof(Student));
} ///
向数据表添加数据 /// public void InsertTable() { Student stu = new Student(); = "张三"; = 20; //1.添加实例对象 able(stu).ExecuteCommand(); //2.添加lst集合 List
数据查询 /// public void SelectTable() { //1.条件查询 List
分页查询 var query = ble
数据修改 /// public void UpdateTable() { //1.更新Name和Age,条件为Name为“张张三”并且age为5 var t1 = able
根据条件修改 able(student).Where(t => == 10).ExecuteCommand(); //3.
根据主键更新数据 able(student).ExecuteCommand(); } ///
删除数据 /// public void DeleteTable() { //1.删除所有数据 able
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689244753a225557.html
评论列表(0条)