.NET ORM工具Pax实战Pax插入表到数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET ORM工具Pax实战Pax插入表到数据库相关的知识,希望对你有一定的参考价值。
Pax是微软的一个ORM读写工具,比NHibernate更好用。可以在项目中用Nuget下载并安装。
可以看到引用中多了
在App.config里设置配置文件如下
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <connectionStrings> <clear/> <add name="Couriers" connectionString="Server=.;User id=sa;password=3179412;Initial Catalog=Couriers;MultipleActiveResultSets=true;Type System Version=SQL Server 2012;" /> </connectionStrings> </configuration>
写一个建立与sqlserver2012连接的工具类
public class BaseDb { public IDbConnection GetDb( IOrmLiteDialectProvider provider = null) { var connections = Pax.Lib.Configuration.ConfigUtils.GetConnectionString(ConnectionStr); //??主要作用是如果 ?? 运算符的左操作数非空,该运算符将返回左操作数,否则返回右操作数。 var factory = new OrmLiteConnectionFactory(connections, provider??SqlServerDialect.Provider); return factory.Open(); } public BaseDb(string connectionStr) { ConnectionStr = connectionStr; } private string ConnectionStr; public IDbConnection Db => GetDb(); ~BaseDb()//析构函数 { if (Db.State == ConnectionState.Open) { Db.Close(); Db.Dispose(); } } }
连接前Couriers数据库里什么也没有。
建立一个类,类的字段要对应数据库的字段。
public class ModelWithIdOnly: { public ModelWithIdOnly() { } public ModelWithIdOnly(long id) { Id = id; } // must be long as you cannot have a table with only an autoincrement field public long Id { get; set; } }
在入口处写程序如下:
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; Db.DropAndCreateTable(typeof(ModelWithIdOnly)); var success = Db.TableExists(typeof (ModelWithIdOnly).Name.SqlTableRaw()); if (success) { Console.WriteLine("ModelWithIdOnly表插入到了数据库"); Console.ReadKey(); } } }
运行可以看到
表的删除
static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; Db.DropTable<ModelWithIdOnly>(); var success = Db.TableExists(typeof(ModelWithIdOnly).Name.SqlTableRaw());//检查新表是否存在 if (!success) { Console.WriteLine("ModelWithIdOnly表在数据库中删除"); Console.ReadKey(); } }
表的新增
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; Db.CreateTable<ModelWithIdOnly>(); var success = Db.TableExists(typeof(ModelWithIdOnly).Name.SqlTableRaw());//检查新表是否存在 if (success) { Console.WriteLine("ModelWithIdOnly表在数据库中新增"); Console.ReadKey(); } } }
插入一列
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; var success= Db.Insert(new ModelWithIdOnly(1)); if (success>0) { Console.WriteLine("成功插入了{0}列",success); Console.ReadKey(); } } }
返回表中所有数据
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; var rows = Db.Select<ModelWithIdOnly>(); if (rows.Count>0) { Console.WriteLine(rows); Console.ReadKey(); } } }
输出sql语句
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; var rows = Db.Select<ModelWithIdOnly>(); var strSQL = Db.GetLastSql(); if (rows.Count>0) { Console.WriteLine(strSQL); Console.ReadKey(); } } }
创建新类
public class ModelWithGuid { public long Id { get; set; } public Guid Guid { get; set; } }
简单的插入与查找
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.Db; Db.DropAndCreateTable<ModelWithGuid>(); var models = new[] { new ModelWithGuid { Id = 1, Guid = Guid.NewGuid() }, new ModelWithGuid { Id = 2, Guid = Guid.NewGuid() } }; var successNum= Db.SaveAll(models); string strSql1 = Db.GetLastSql();//获得sql语句 var newModel = Db.SingleById<ModelWithGuid>(models[0].Id);//取出某列
var newModel2 = Db.Single<ModelWithGuid>(q => q.Guid == models[0].Guid);//取出某列
Db.Update(new ModelWithGuid { Id = models[0].Id, Guid = newGuid });//更新某列
bool has = Db.TableExists(typeof (ModelWithIdOnly).Name.SqlTableRaw());//某表是否存在
var model = new[] {
new ModelWithGuid { Id = 3, Guid = Guid.NewGuid() }
};
long num= Db.Insert(model);//插入某列,返回插入列数
}
}
执行sql语句插class Program
{ static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.GetDb(); var models = new[] { new ModelWithGuid { Id = 4, Guid = Guid.NewGuid() }, new ModelWithGuid { Id =5, Guid = Guid.NewGuid() } }; Db.ExecuteSql("INSERT INTO {0} ({1},{2}) VALUES (4,{3})" .Fmt("ModelWithGuid".SqlTable(), "Id".SqlColumn(), "Guid".SqlColumn(), models[0].Guid.SqlValue())); var normalLastInsertedId = Db.LastInsertId();
var rows = Db.Select<ModelWithGuid>();
var row2 = rows.First(x => x.Id == 4);
var rowIds = new List<int>(new[] { 1, 2, 3 });
rows = Db.SelectByIds<ModelWithGuid>(rowIds);
var dbRowIds = rows.ConvertAll(x => x.Id);
Console.WriteLine(normalLastInsertedId.ToString());
Console.ReadKey();
}
}
批量删除
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.GetDb(); var rowIds = new List<int>(new[] { 1, 2 }); var success= Db.DeleteByIds<ModelWithGuid>(new[] { rowIds[0], rowIds[1] }); Console.WriteLine(success); Console.ReadKey(); } }
选择性删除
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.GetDb(); var rowIds = new List<int>(new[] { 1, 2 }); var success= Db.DeleteByIds<ModelWithGuid>(new[] { rowIds[0], rowIds[1] }); Db.Delete<ModelWithGuid>(x => x.Id <= 4); var dbRow = Db.SingleById<ModelWithGuid>(4); Console.WriteLine(dbRow==null?"没有":dbRow.Guid.ToString()); Console.ReadKey(); } }
条件删除
class Program { static void Main(string[] args) { BaseDb baseDb = new BaseDb("Couriers"); var Db = baseDb.GetDb(); Db.DeleteFmt<ModelWithIdOnly>(where: "Id".SqlColumn() + " < {0}".SqlFmt(2)); var dbRow = Db.SingleById<ModelWithIdOnly>(1); Console.WriteLine(dbRow==null?"没有":dbRow.Id.ToString()); Console.ReadKey(); } }
插入与删除
class Program { static void Main(string[] args) { var connections = Pax.Lib.Configuration.ConfigUtils.GetConnectionString("Couriers"); var factory = new OrmLiteConnectionFactory(connections, SqlServerDialect.Provider); IDbConnection Db = factory.Open(); using (Db) { ModelWithIdOnly one = new ModelWithIdOnly {Id = 10}; Db.Save(one); var rowsAffected = Db.Delete(one); Console.WriteLine(rowsAffected.ToString()); Console.ReadKey(); } } }
批量增删
class Program { static void Main(string[] args) { var connections = Pax.Lib.Configuration.ConfigUtils.GetConnectionString("Couriers"); var factory = new OrmLiteConnectionFactory(connections, SqlServerDialect.Provider); IDbConnection Db = factory.Open(); using (Db) { List <ModelWithIdOnly> ones = new List<ModelWithIdOnly>(){ new ModelWithIdOnly { Id = 8},new ModelWithIdOnly{Id=11} }; Db.SaveAll(ones); var rowsAffected = Db.Delete(ones.ToArray()); Console.WriteLine(rowsAffected.ToString()); Console.ReadKey(); } } }
按条件更新
以上是关于.NET ORM工具Pax实战Pax插入表到数据库的主要内容,如果未能解决你的问题,请参考以下文章
pax-jdbc PostgreSQL JBoss Fuse
BitMart币市上线Paxos Standard(PAX)