标签:xcode 设置 统计 wal 复数 net 高级 arch stl
NewLife.XCode是一个有10多年历史的开源数据中间件,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode。
整个系列教程会大量结合示例代码和运行日志来进行深入分析,蕴含多年开发经验于其中。
开源地址:https://github.com/NewLifeX/X
新建控制台项目(.NET Core),从nuget引用NewLife.XCode,(也可以从别的项目引用下载后拷贝XCode.dll和NewLife.Core.dll来使用,区分nfx和netcore版本),敲入以下代码
// 启用控制台日志 XTrace.UseConsole(); // 添加 var user = new UserX { Name = "大石头", Enable = true }; user.Insert(); // 自增字段user.ID已经取得值 XTrace.WriteLine("用户ID:{0}", user.ID); // 查询 var user2 = UserX.Find(UserX._.Name == "大石头"); // 更新 user2.Logins++; user2.LastLogin = DateTime.Now; user2.Update(); // 删除 var user3 = UserX.FindByID(user.ID); user3.Delete(); Console.ReadLine();
先跑起来看看效果:
#Software: ConsoleApp4 #ProcessID: 21252 x64 #AppDomain: ConsoleApp4 #FileName: C:\Program Files\dotnet\dotnet.exe #BaseDirectory: D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1#CommandLine: D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\ConsoleApp4.dll #ApplicationType: Console #CLR: 4.0.30319.42000, #OS: Microsoft Windows NT 6.2.9200.0, X3/Stone #CPU: 8 #GC: IsServerGC=False, LatencyMode=Interactive #Date: 2019-02-25 #字段: 时间 线程ID 线程池Y网页W普通N 线程名 消息内容 #Fields: Time ThreadID IsPoolThread ThreadName Message 22:57:13.089 1 N - NewLife.Core v8.0.2019.0101 Build 2019-01-26 23:22:36 22:57:13.104 1 N - 新生命X组件核心库 ©2002-2019 新生命开发团队 22:57:13.114 1 N - ConsoleApp4 v1.0.0.0 Build 2000-01-01 22:57:13.115 1 N - ConsoleApp4 22:57:13.115 1 N - Setting的配置文件Config\Core.config 不存在,准备用默认配置覆盖! 22:57:13.277 1 N - Setting的配置文件Config\XCode.config 不存在,准备用默认配置覆盖! 22:57:13.282 1 N - XCode v9.10.2019.0101 Build 2019-01-26 23:22:38 22:57:13.282 1 N - 数据中间件 ©2002-2019 新生命开发团队 22:57:13.628 1 N - 自动为[Membership]设置SQLite连接字符串:Data Source=.\Membership.db;Migration=On 22:57:13.652 1 N - System.Data.SQLite.dll不存在或平台版本不正确,准备联网获取 http://x.newlifex.com/ 22:57:13.835 1 N - 下载链接 http://x.newlifex.com/,目标 System.Data.SQLite.win-x64,System.Data.SQLite.win,System.Data.SQLite.st,System.Data.SQLite 22:57:13.964 4 Y 1 WebClientX.Get http://x.newlifex.com/ 22:57:15.660 1 N - 分析得到文件 System.Data.SQLite.win-x64_20180823112512.zip,准备下载 http://x.newlifex.com/System.Data.SQLite.win-x64_20180823112512.zip 22:57:15.663 4 Y 19 WebClientX.Get http://x.newlifex.com/System.Data.SQLite.win-x64_20180823112512.zip 22:57:16.897 1 N - 下载完成,共924,475字节,耗时1,232毫秒 22:57:16.897 1 N - 解压缩到 D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\Plugins 22:57:17.074 1 N - TypeX.GetType("System.Data.SQLite.SQLiteFactory") => Plugins\System.Data.SQLite.dll 22:57:17.074 1 N - System.Data.SQLite驱动D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\Plugins\System.Data.SQLite.dll 版本v1.0.109.0 22:57:17.145 1 N - Data Source=D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\Membership.db;Cache Size=-524288;Synchronous=Off;Journal Mode=WAL 22:57:17.154 4 Y 198 检查实体XCode.Membership.UserX的数据表架构,模式:CheckTableWhenFirstUse 22:57:17.160 4 Y 198 创建数据库:Membership 22:57:17.161 4 Y 198 创建数据库:D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\Membership.db 22:57:17.181 4 Y 198 select * from sqlite_master 22:57:17.284 4 Y 198 创建表:User(用户) 22:57:17.298 4 Y 198 Create Table User( ID integer Primary Key AUTOINCREMENT, Name nvarchar(50) NOT NULL COLLATE NOCASE, Password nvarchar(50) NULL COLLATE NOCASE, DisplayName nvarchar(50) NULL COLLATE NOCASE, Sex int NOT NULL, Mail nvarchar(50) NULL COLLATE NOCASE, Mobile nvarchar(50) NULL COLLATE NOCASE, Code nvarchar(50) NULL COLLATE NOCASE, Avatar nvarchar(200) NULL COLLATE NOCASE, RoleID int NOT NULL, RoleIDs nvarchar(200) NULL COLLATE NOCASE, DepartmentID int NOT NULL, Online bit NOT NULL, Enab...etime NULL, LastLoginIP nvarchar(50) NULL COLLATE NOCASE, RegisterTime datetime NULL, RegisterIP nvarchar(50) NULL COLLATE NOCASE, Ex1 int NOT NULL, Ex2 int NOT NULL, Ex3 real NOT NULL, Ex4 nvarchar(50) NULL COLLATE NOCASE, Ex5 nvarchar(50) NULL COLLATE NOCASE, Ex6 nvarchar(50) NULL COLLATE NOCASE, UpdateUser nvarchar(50) NULL COLLATE NOCASE, UpdateUserID int NOT NULL, UpdateIP nvarchar(50) NULL COLLATE NOCASE, UpdateTime datetime NULL, Remark nvarchar(200) NULL COLLATE NOCASE ) 22:57:17.309 4 Y 198 Create Unique Index IU_User_Name On User (Name) 22:57:17.311 4 Y 198 Create Index IX_User_RoleID On User (RoleID) 22:57:17.312 4 Y 198 Create Index IX_User_UpdateTime On User (UpdateTime) 22:57:17.320 4 Y 198 UserX.Count 快速计算表记录数(非精确)[User/Membership] 参考值 -2 22:57:17.332 4 Y 198 开始检查连接[Membership/SQLite]的数据库架构…… 22:57:17.355 4 Y 198 [Membership]的所有实体类(5个):Parameter,UserX,Menu,Role,Department 22:57:17.356 4 Y 198 [Membership]需要检查架构的实体类(1个):Parameter 22:57:17.357 4 Y 198 Membership待检查表架构的实体个数:1 22:57:17.358 4 Y 198 select * from sqlite_master 22:57:17.365 4 Y 198 创建表:Parameter(字典参数) 22:57:17.368 4 Y 198 Create Table Parameter( ID integer Primary Key AUTOINCREMENT, Category nvarchar(50) NULL COLLATE NOCASE, Name nvarchar(50) NULL COLLATE NOCASE, Value nvarchar(200) NULL COLLATE NOCASE, LongValue nvarchar(2000) NULL COLLATE NOCASE, Kind int NOT NULL, Enable bit NOT NULL, Ex1 int NOT NULL, Ex2 int NOT NULL, Ex3 real NOT NULL, Ex4 nvarchar(50) NULL COLLATE NOCASE, Ex5 nvarchar(50) NULL COLLATE NOCASE, Ex6 nvarchar(50) NULL COLLATE NOCASE, CreateUser nvarchar(50) NULL COLLATE NOCASE, CreateUserID int NOT NULL, CreateIP nvarchar(50) NULL COLLATE NOCASE, CreateTime datetime NULL, UpdateUser nvarchar(50) NULL COLLATE NOCASE, UpdateUserID int NOT NULL, UpdateIP nvarchar(50) NULL COLLATE NOCASE, UpdateTime datetime NULL, Remark nvarchar(200) NULL COLLATE NOCASE ) 22:57:17.369 4 Y 198 Create Unique Index IU_Parameter_Category_Name On Parameter (Category, Name) 22:57:17.370 4 Y 198 Create Index IX_Parameter_Name On Parameter (Name) 22:57:17.370 4 Y 198 Create Index IX_Parameter_UpdateTime On Parameter (UpdateTime) 22:57:17.372 4 Y 198 检查连接[Membership/SQLite]的数据库架构耗时38ms 22:57:17.383 4 Y 198 Select * From User Order By ID Desc limit 1 22:57:17.388 4 Y 198 Select Count(*) From User 22:57:17.398 4 Y 198 Select Count(*) From User 22:57:17.401 4 Y 198 开始初始化UserX用户数据…… 22:57:17.455 4 Y 198 Insert Into User(Name, Password, DisplayName, Sex, Mail, Mobile, Code, Avatar, RoleID, RoleIDs, DepartmentID, Online, Enable, Logins, LastLogin, LastLoginIP, RegisterTime, RegisterIP, Ex1, Ex2, Ex3, Ex4, Ex5, Ex6, UpdateUser, UpdateUserID, UpdateIP, UpdateTime, Remark) Values(‘admin‘, ‘21232F297A57A5A743894A0E4A801FC3‘, ‘管理员‘, 0, null, null, null, null, 1, ‘‘, 0, 0, 1, 0, null, null, null, null, 0, 0, 0, null, null, null, null, 0, null, null, null);Select last_insert_rowid() newid 22:57:17.479 4 Y 198 自动为[Log]设置SQLite连接字符串:Data Source=.\Log.db;Migration=On 22:57:17.491 4 Y 198 Data Source=D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\Log.db;Cache Size=-524288;Synchronous=Off;Journal Mode=WAL 22:57:17.514 4 Y 198 完成初始化UserX用户数据! 22:57:17.548 1 N - Insert Into User(Name, Password, DisplayName, Sex, Mail, Mobile, Code, Avatar, RoleID, RoleIDs, DepartmentID, Online, Enable, Logins, LastLogin, LastLoginIP, RegisterTime, RegisterIP, Ex1, Ex2, Ex3, Ex4, Ex5, Ex6, UpdateUser, UpdateUserID, UpdateIP, UpdateTime, Remark) Values(‘大石头‘, null, null, 0, null, null, null, null, 0, null, 0, 0, 1, 0, null, null, null, null, 0, 0, 0, null, null, null, null, 0, null, null, null);Select last_insert_rowid() newid 22:57:17.561 1 N - 用户ID:2 22:57:17.574 1 N - Select * From User Where Name=‘大石头‘ Order By ID Desc limit 1 22:57:17.601 1 N - Update User Set Logins=Logins+1, LastLogin=‘2019-02-25 22:57:17‘ Where ID=2 22:57:17.613 1 N - Select * From User Order By ID Desc 22:57:17.620 1 N - Delete From User Where ID=2 22:57:18.519 6 Y 199 检查实体XCode.Membership.Log的数据表架构,模式:CheckTableWhenFirstUse 22:57:18.520 6 Y 199 创建数据库:Log 22:57:18.521 6 Y 199 创建数据库:D:\Test\ConsoleApp4\ConsoleApp4\bin\Debug\netcoreapp2.1\Log.db 22:57:18.532 6 Y 199 创建表:Log(日志) 22:57:18.537 4 Y EQ 开始检查连接[Log/SQLite]的数据库架构…… 22:57:18.555 4 Y EQ [Log]的所有实体类(3个):Log,UserOnline,VisitStat 22:57:18.555 4 Y EQ [Log]需要检查架构的实体类(2个):UserOnline,VisitStat 22:57:18.558 4 Y EQ Log待检查表架构的实体个数:2 22:57:18.559 4 Y EQ 创建表:UserOnline(用户在线) 22:57:18.561 4 Y EQ 创建表:VisitStat(访问统计) 22:57:18.564 4 Y EQ 检查连接[Log/SQLite]的数据库架构耗时24ms
这是前述代码的完整执行日志,这里我们不做过多分析。
日志大意是:自动配置SQLite数据库,下载平台要求的SQLite驱动,建库建表,增删改查
这里特别需要关注的是这几行:
22:57:17.548 1 N - Insert Into User(Name, Password, DisplayName, Sex, Mail, Mobile, Code, Avatar, RoleID, RoleIDs, DepartmentID, Online, Enable, Logins, LastLogin, LastLoginIP, RegisterTime, RegisterIP, Ex1, Ex2, Ex3, Ex4, Ex5, Ex6, UpdateUser, UpdateUserID, UpdateIP, UpdateTime, Remark) Values(‘大石头‘, null, null, 0, null, null, null, null, 0, null, 0, 0, 1, 0, null, null, null, null, 0, 0, 0, null, null, null, null, 0, null, null, null);Select last_insert_rowid() newid 22:57:17.561 1 N - 用户ID:2 22:57:17.574 1 N - Select * From User Where Name=‘大石头‘ Order By ID Desc limit 1 22:57:17.601 1 N - Update User Set Logins=Logins+1, LastLogin=‘2019-02-25 22:57:17‘ Where ID=2 22:57:17.613 1 N - Select * From User Order By ID Desc 22:57:17.620 1 N - Delete From User Where ID=2
一句user.Insert,生成了一个Insert Into语句,并取得自增字段赋值给ID。
一个简单的Find单字段查找,通过user2.Update更新了两个字段,user3.Delete则删除了这行数据。
这就是一个完成的增删改查!
短短的一段代码,也许带给你许多疑问,这些疑问都将在后续文章中得到详细解答:
NewLife.XCode教程系列[2019版]
End.
标签:xcode 设置 统计 wal 复数 net 高级 arch stl
原文地址:https://www.cnblogs.com/nnhy/p/xcode_curd.html