博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ServiceStack.OrmLite 基本操作
阅读量:5232 次
发布时间:2019-06-14

本文共 6109 字,大约阅读时间需要 20 分钟。

原文:https://www.cnblogs.com/wang2650/category/780821.html

原文:https://www.cnblogs.com/xxfcz/p/7045808.html

 

 

注意一下环境哦,这里我使用的版本是 4.0.62,不同版本语法上肯定是有差异的,

还有就是为什么装的是个老版本,本地的VS装不了最新的版本,新版本好像要用到Core 2.0。以后再试试最近的版本了,空了来。

 

 

 

 

全部代码:

using ServiceStack;using ServiceStack.DataAnnotations;using ServiceStack.OrmLite;using ServiceStack.Text;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp2.test1{    public class Class12    {        //ServiceStack.OrmLite        //原文:https://www.cnblogs.com/xxfcz/p/7045808.html                //原文:https://www.cnblogs.com/wang2650/category/780821.html        public void test1()        {            //查询数据            string connString = "data source=.\\sqlexpress;initial catalog=MyDbContext;user id=sa;password=sa;";            var dbFactory = new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider);            using (var db = dbFactory.Open())            {                var roles = db.Select
(); "Roles: {0}".Print(roles.Dump());//输出到控制台 } } public void test2() { //创建表,如果表已存在,那么删除后再创建 OrmLiteConfig.DialectProvider = SqlServerDialect.Provider; string connString = "data source=.\\sqlexpress;initial catalog=MyDbContext;user id=sa;password=sa;"; using (var db = connString.OpenDbConnection()) { db.DropAndCreateTable
(); } } public void test3() { //创建表,并插入一条数据。表如果已存在,则不插入数据 string connectionString = "data source=.\\sqlexpress;initial catalog=MyDbContext;user id=sa;password=sa;"; var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider); using (var db = dbFactory.Open()) { if (db.CreateTableIfNotExists
()) { db.Insert(new Book2 { Id = 1, Name = "Seed Data" }); } } } public void test4() { //插入 string connectionString = "data source=.\\sqlexpress;initial catalog=MyDbContext;user id=sa;password=sa;"; var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider); using (var db = dbFactory.Open()) { //db.CreateTableIfNotExists
(); //db.Insert(new Book2 { Id = 1, Name = "Seed Data1" }); //db.Insert(new Book2 { Id = 2, Name = "Seed Data2" }); //db.Insert(new Book2 { Id = 3, Name = "Seed Data3" }); //db.Insert(new Book2 { Id = 4, Name = "Seed Data4" }); //db.Insert(new Book2 { Id = 5, Name = "Seed Data5" }); ////异步 新增 //Task
task = db.InsertAsync(new Book2 { Id = 6, Name = "Seed Data6" }); //Console.WriteLine("The Sum is:" + task.Result); ////只新增 指定字段 //db.InsertOnly
(new Book2 { Id = 6, Name = "Seed Data6" }, c => c.Id); ////新增数据 返回自增长ID。 这里需要修改一下环境,数据库表设置主键、设置自增长,Model的Id增加特性AutoIncrement、PrimaryKey //long id = db.Insert(new Book2 { Name = "Seed Data5" }, selectIdentity: true); //Console.WriteLine(id); ////插入数据的时候,自动插入创建时间等 通用字段 ////全局的 插入时 过滤 //OrmLiteConfig.InsertFilter = (dbCmd, row) => { // var auditRow = row as BaseEntity; // if (auditRow != null) // auditRow.CreatedDate = auditRow.ModifiedDate = DateTime.UtcNow; //}; ////db.CreateTableIfNotExists
(); //db.Insert(new Book3 { Id = 3, Name = "Seed Data3" }); //db.Insert(new Book3 { Id = 4, Name = "Seed Data4" }); //db.Insert(new Book3 { Id = 5, Name = "Seed Data5" }); ////直接执行sql语句 //db.ExecuteSql("INSERT INTO Book2 (Id, Name) VALUES (@Id, @Name)", new Book2 { Id = 5, Name = "Seed Data5" }); //批量添加 int i = 0; var rows = "A,B,B,C,C,C,D,D,E".Split(',').Map(x => new Book2 { Name = x, Id = (i++) }); db.InsertAll(rows); } } public void test5() { //删除 string connectionString = "data source=.\\sqlexpress;initial catalog=MyDbContext;user id=sa;password=sa;"; var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider); using (var db = dbFactory.Open()) { //db.DeleteById
(0); //db.Delete
(new Book2 { Id = 1, Name = "B" }); //db.Delete
(p => p.Name == "D"); ////这里没有测试成功,原因未知 //var updatedRow = db.SingleById
(1); // fresh version //db.DeleteById
(id: updatedRow.Id, rowVersion: updatedRow.RowVersion); } } public void test6() { string connectionString = "data source=.\\sqlexpress;initial catalog=MyDbContext;user id=sa;password=sa;"; var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider); using (var db = dbFactory.Open()) { //db.Update(new Book2 { Id = 2, Name="BB" }); // sql:UPDATE "Person" SET "FirstName" = 'Jimi',"LastName" = 'Hendrix',"Age" = 27 WHERE "Id" = 1 //db.Update(new Book2 { Id=1, Name = "CCCC" }, p => p.Name == "BBCCB"); //Console.WriteLine(db.GetLastSql()); ////部分更新 //db.UpdateNonDefaults(new Book2 { Name = "CC" }, p => p.Name == "CCCC"); ////部分更新 //db.UpdateOnly(new Book2 { Name = "DD" }, //onlyFields: p => new { p.Name }, //where: p => p.Id == 1); //先获取 再更新 var updatedRow = db.SingleById
(1); updatedRow.Name = "AA"; db.Update(updatedRow); //打印SQL语句 Console.WriteLine(db.GetLastSql()); } } public void test7() { } public class Book4 { public int Id { get; set; } public string Name { get; set; } public ulong RowVersion { get; set; } } public class BaseEntity { public int Id { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public string ModifiedBy { get; set; } } public class Book3 : BaseEntity { public string Name { get; set; } } [Serializable] [Alias("Book2")] public class Book2 { //[AutoIncrement] //[PrimaryKey] public int Id { get; set; } public string Name { get; set; } } [Serializable] [Alias("Book")] public class Book { public int Id { get; set; } public string Name { get; set; } } }}

 

转载于:https://www.cnblogs.com/guxingy/p/11195045.html

你可能感兴趣的文章
中文系统 上传file的input显示英文
查看>>
比callback更简洁的链式执行promise
查看>>
android permission
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>
Python 第四十五章 MySQL 内容回顾
查看>>
实验2-2
查看>>
MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
查看>>
android smack MultiUserChat.getHostedRooms( NullPointerException)
查看>>
监控工具之---Prometheus 安装详解(三)
查看>>
不错的MVC文章
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
路冉的JavaScript学习笔记-2015年1月23日
查看>>
Mysql出现(10061)错误提示的暴力解决办法
查看>>
2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
查看>>
Swift - 异步加载各网站的favicon图标,并在单元格中显示
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>