最好的网站设计公,腾讯云改wordpress,制作h5网页流程及详细步骤,id设计目录
一、配置 二、操作步骤
1、根据配置映射数据库对象
2、实体配置
3、创建表
4、增删改查
增加数据
删除数据
更新数据
查询数据
5、导航增删改查
增加数据
删除数据
更新数据
查询数据
6、雪花ID
三、工具
SqlLite可视化工具
MySQL安装包
MySQL可视化…目录
一、配置 二、操作步骤
1、根据配置映射数据库对象
2、实体配置
3、创建表
4、增删改查
增加数据
删除数据
更新数据
查询数据
5、导航增删改查
增加数据
删除数据
更新数据
查询数据
6、雪花ID
三、工具
SqlLite可视化工具
MySQL安装包
MySQL可视化工具
SqlServer安装 SqlSugar官方文档https://www.donet5.com/Home/Doc?typeId2308
一、配置
1、Nuget包添加SqlSugar
2、App.config添加配置
?xml version1.0 encodingutf-8 ?
configurationstartup supportedRuntime versionv4.0 sku.NETFramework,Versionv4.7.2 //startupconnectionStrings!--sqlite数据库字符串路径符号|DataDirectory|代表当前运行目录--add namesqlite providerNameSystem.Data.SQLite connectionStringData Source|DataDirectory|\TestData.db;Version3; /!--Sqlserver数据库的连接字符串--add namesqlserver providerNameSystem.Data.SqlClient connectionStringPersist Security InfoFalse;Data Source(local);Initial CatalogTestData;Integrated SecuritySSPI /!--MySQL数据库的连接字符串--add namemysql providerNameMySql.Data.MySqlClient connectionStringServerlocalhost;DatabaseTestData;Uidroot;Pwd123456;SslModenone /!--PostgreSQL数据库的连接字符串--add namenpgsql providerNameNpgsql connectionStringServerlocalhost;Port5432;DatabaseTestData;User Idroot;Password123456 /!--不受驱动影响32位64位均可使用--add nameoracle providerNameOracleManaged connectionStringData Source(DESCRIPTION(ADDRESS(PROTOCOLTCP)(HOSTlocalhost)(PORT1521))(CONNECT_DATA(SERVERDEDICATED)(SERVICE_NAMEorcl)));User IDauston;Password123456 /!--达梦数据库的连接字符串--add nameDm providerNameDm connectionStringServerlocalhost;User IDauston;PWD123456;DatabaseCSPData; //connectionStringsappSettings!--指定默认的数据库类型如果不指定则使用第一个连接字符串--add keyDbType valuesqlite /add keyClientSettingsProvider.ServiceUri value //appSettings
/configuration 二、操作步骤
1、根据配置映射数据库对象 private void Connect(){try{var db ConfigurationManager.AppSettings.Get(DbType);var connectStr ConfigurationManager.ConnectionStrings[db].ConnectionString;DbType dbType DbType.Sqlite;switch (db){case sqlite:dbType DbType.Sqlite;break;case mysql:dbType DbType.MySql;break;case sqlserver:dbType DbType.SqlServer;break;}sqlSugarScope new SqlSugarScope(new ConnectionConfig(){ConnectionString connectStr,DbType dbType,IsAutoCloseConnection true,//自动释放数据务如果存在事务在事务结束后释放InitKeyType InitKeyType.Attribute,//从实体特性中读取主键自增列信息});}catch (Exception){}}
2、实体配置
TableName指定表名不指定默认类名
ColumnName指定列名不指定默认属性名
IsPrimaryKey是否设为主键 [SugarTable(TableName Student)]public class Student{[SugarColumn(ColumnName ID,IsPrimaryKey true)]public string Id { get; set; }[SugarColumn(ColumnName Name)]public string Name { get; set; }}
3、创建表 private void CreateTable(int len, params Type[] types){//设置varchar的默认长度sqlSugarScope.CodeFirst.SetStringDefaultLength(len);//sqlSugarScope.CodeFirst.BackupTable().InitTables(types);//备份表sqlSugarScope.CodeFirst.InitTables(types);}
4、增删改查
增加数据 private void AddOne(Student stu){sqlSugarScope.InsertableStudent(stu).ExecuteCommand();} 删除数据
private void Delete(int id)
{sqlSugarScope.DeleteableStudent().Where(ss.Id.Equals(id)).ExecuteCommand();
}
更新数据 private void Update(Student stu){//根据主键更新sqlSugarScope.UpdateableStudent(stu).ExecuteCommand();//据主键更新指定列//sqlSugarScope.UpdateableStudent(stu).UpdateColumns(i new { i.Id,i.Name}).ExecuteCommand();//根据指定列更新//sqlSugarScope.UpdateableStudent(stu).WhereColumns(inew { i.Name}).ExecuteCommand();//根据指定条件更新//sqlSugarScope.UpdateableStudent(stu).Where(i i.Age.Equals(18)).ExecuteCommand();//据主键更新忽略指定列//sqlSugarScope.UpdateableStudent(stu).IgnoreColumns(inew { i.Age}).ExecuteCommand();}
查询数据
var stus sqlSugarScope.QueryableStudent().Where(i i.Age.Equals(22)).ToList();
5、导航增删改查
增加数据
NavigateType指定导航类型
nameof()绑定Id用于导航
IsIdentity是否自增 private void AddNav(){var books1 new ListBook(){new Book(){ NameBookA},new Book(){ NameBookB},new Book(){ NameBookC},};var books2 new ListBook(){new Book(){ NameBookK},new Book(){ NameBookP},new Book(){ NameBookZ},};sqlSugarScope.InsertNavStudent(new Student() { Name GGBom, Books books1 }).Include(i i.Books).ExecuteCommand();sqlSugarScope.InsertNavStudent(new Student() { Name LuBi, Books books2 }).Include(i i.Books).ExecuteCommand();}[SugarTable(TableName Student)]public class Student{[SugarColumn(ColumnName ID, IsPrimaryKey true, IsIdentity true)]public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }[Navigate(NavigateType.OneToMany, nameof(Book.StudentId))]public ListBook Books { get; set; }}public class Book{[SugarColumn( IsPrimaryKey true, IsIdentity true)]public int BookId { get; set; }public string Name { get; set; }public int StudentId { get; set; }}
删除数据
private void DeleteNav(int age)
{sqlSugarScope.DeleteNavStudent(i i.Age.Equals(age)).Include(m m.Books).ExecuteCommand();
}
更新数据 private void UpdateNav(){var books new ListBook(){new Book(){ NameBookNew1},new Book(){ NameBookNew2},new Book(){ NameBookNew3},};sqlSugarScope.UpdateNavStudent(new Student() {Id1, NameLucy,Booksbooks}).Include(i i.Books).ExecuteCommand();}
查询数据
var stus sqlSugarScope.QueryableStudent().Where(i i.Age.Equals(22)).Includes(i i.Books).ToList();
6、雪花ID
设置WorkId //程序启时动执行一次就行//从配置文件读取一定要不一样//服务器时间修改一定也要修改WorkIdSnowFlakeSingle.WorkId 1;long类型主键自动赋值
[SugarColumn(ColumnName ID, IsPrimaryKey true)]
public long Id { get; set; }//long类型的主键会自动赋值
long没有19位长度序列化雪花ID时要序列化成string
[Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName ID, IsPrimaryKey true)]
public long Id { get; set; }//long类型的主键会自动赋值 插入返回雪花ID
long id db.Insertable(实体).ExecuteReturnSnowflakeId();//单条插入返回雪花ID
ListLong idsdb.Insertable(List实体).ExecuteReturnSnowflakeIdList();//多条插入批量返回,比自增好用 手动调雪花ID
var idSnowFlakeSingle.Instance.NextId();//也可以在程序中直接获取ID
自定义雪花算法 //程序启动时执行一次就行StaticConfig.CustomSnowFlakeFunc () {return 你的雪花ID方法();}; 三、工具
SqlLite可视化工具
链接 https://pan.baidu.com/s/1gCkYh2lxduUUKFIj5HHH8w
提取码: xvsc
MySQL安装包
链接: https://pan.baidu.com/s/1X9HCtp4sMI9C0XAnBtpi5A
提取码: 97uh
MySQL可视化工具
链接: https://pan.baidu.com/s/1ij42YorBtK96gwhLVNeopw
提取码: 1afx
SqlServer安装
链接: https://pan.baidu.com/s/1od-s97LzlqrUnX3o8inoJQ
提取码: i5sj