Agile.DataAccess.dll 文件为平台数据访问支持库,本ORM基于FluentData扩展重写,提供各种数据库访问功能。
数据库支持列表:
MS SQL Server using the native .NET driver.
MS SQL Azure using the native .NET driver.
MS Access using the native .NET driver.
MS SQL Server Compact 4.0 through the Microsoft SQL Server Compact 4.0 driver.
Oracle through the ODP.NET driver.
MySQL through the MySQL Connector .NET driver.
SQLite through the SQLite ADO.NET Data Provider.
PostgreSql through the Npgsql provider.
IBM DB2
Sybase through the https://github.com/FredoKapo/FLUENT-ORM-ASE-PROVIDER provider.
API示例
查询返回动态对象列表
List<dynamic> products = DataContext.Script("select * from Product")
.QueryMany<dynamic>();
查询返回强类型对象列表 List<Product> products = DataContext.Script("select * from Product")
.QueryMany<Product>();
查询返回单个动态对象
dynamic product = DataContext.Script(@"select * from Product where ProductId = 1").QuerySingle<dynamic>();
查询返回单个强类型对象
Product product = DataContext.Script(@"select * from Product where ProductId = 1").QuerySingle<Product>();
查询返回数据表
DataTable products = DataContext.Script("select * from Product")
.QuerySingle<DataTable>();
查询一个标量值
int number = DataContext.Script(@"select count(*) from Product")
.QuerySingle<int>();
查询一个标量值的列表
List<int> productIds = DataContext.Script(@"select ProductId from Product").QueryMany<int>();
自定义匹配实体类
List<Product> products = DataContext.Script(@"select p.*,
c.CategoryId as Category_CategoryId,
c.Name as Category_Name
from Product p
inner join Category c on p.CategoryId = c.CategoryId")
.QueryMany<Product>();
自定义映射
List<Product> products = DataContext.Script(@"select * from Product")
.QueryMany<Product>(Custom_mapper_using_dynamic);
public void Custom_mapper_using_dynamic(Product product, dynamic row)
{
product.ProductId = row.ProductId;
product.Name = row.Name;
}
自定义映射使用DataReader
List<Product> products = DataContext.Script(@"select * from Product")
.QueryMany<Product>(Custom_mapper_using_datareader);
public void Custom_mapper_using_datareader(Product product, IDataReader row)
{
product.ProductId = row.GetInt32("ProductId");
product.Name = row.GetString("Name");
}
查询参数的应用
方式1
dynamic products = DataContext.Script(@"select * from Product where ProductId = @0 or ProductId = @1", 1, 2).QueryMany<dynamic>();
方式2
dynamic products = DataContext.Script(@"select * from Product where ProductId = @0 or ProductId = @1").Parameters(1, 2).QueryMany<dynamic>();
方式3(命名参数)
dynamic products = DataContext.Script(@"select * from Product where ProductId = @ProductId1 or ProductId [email protected]").Parameter("ProductId1", 1).Parameter("ProductId2", 2).QueryMany<dynamic>();
方式4(输出参数):
var command = DataContext.Script(@"select @ProductName = Name from Product where ProductId=1").ParameterOut("ProductName", DataTypes.String, 100);
command.Execute();
string productName = command.ParameterValue<string>("ProductName");
分页查询:
List<Product> products = DataContext.Select<Product>("p.*, c.Name as Category_Name").From(@"Product p inner join Category c on c.CategoryId = p.CategoryId ").Where("p.ProductId > 0 and p.Name is not null")
.OrderBy("p.Name").Paging(1, 10).QueryMany();
插入数据
方式1
DataContext.Script(@"insert into Product(Name, CategoryId) values(@0, @1);").Parameters("Way", 1).Execute();
方式2 (插入数据并返回自增主键)
int productId = DataContext.Script(@"insert into Product(Name, CategoryId) values(@0, @1);").Parameters("Way", 1).ExecuteReturnLastId<int>();
方式3 (插入数据并返回自增主键)
int productId = DataContext.Insert("Product")
.Column("Name", "The Warren Buffet Way")
.Column("CategoryId", 1)
.ExecuteReturnLastId<int>();
方式4 (自动构造SQL)
Product product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
product.ProductId = DataContext.Insert<Product>("Product", product)
.AutoMap(x => x.ProductId)
.ExecuteReturnLastId<int>();
更新数据
方式1
int rowsAffected = DataContext.Script(@"update Product set Name = @0 where ProductId = @1").Parameters("The Warren Buffet Way", 1).Execute();
方式2
int rowsAffected = DataContext.Update("Product")
.Column("Name", "The Warren Buffet Way")
.Where("ProductId", 1)
.Execute();
方式3 (自动构造SQL)
int rowsAffected = DataContext.Update<Product>("Product", product)
.AutoMap(x => x.ProductId)
.Where(x => x.ProductId)
.Execute();
插入或更新补填方法
var product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
var insertBuilder = DataContext.Insert<Product>("Product", product).Fill(FillBuilder);
var updateBuilder = DataContext.Update<Product>("Product", product).Fill(FillBuilder);
public void FillBuilder(IInsertUpdateBuilder<Product> builder)
{
builder.Column(x => x.Name);
builder.Column(x => x.CategoryId);
}
删除数据
方式1
int rowsAffected = DataContext.Script(@"delete from Product where ProductId = 1").Execute();
方式2
int rowsAffected = DataContext.Delete("Product")
.Where("ProductId", 1)
.Execute();
方式3
int rowsAffected = DataContext.Delete<Product>("Product", product)
.Where(x => x.ProductId)
.Execute();
存储过程
方式1
var rowsAffected = DataContext.Script("ProductUpdate")
.CommandType(DbCommandTypes.StoredProcedure)
.Parameter("ProductId", 1)
.Parameter("Name", "The Warren Buffet Way")
.Execute();
方式2
var rowsAffected = DataContext.Procedure("ProductUpdate")
.Parameter("Name", "The Warren Buffet Way")
.Parameter("ProductId", 1).Execute();
方式3
var rowsAffected = DataContext.Procedure<Product>("ProductUpdate", product).AutoMap(x => x.CategoryId).Execute();
方式4
var rowsAffected = DataContext.Procedure<Product>("ProductUpdate", product) .Parameter(x => x.ProductId)
.Parameter(x => x.Name).Execute();
事物应用
使用事务时包装里面的代码使用using语句来确保数据库连接关闭。 默认情况下,如果发生任何异常或者不提交将事物自动回滚.
using (var context = DataContext.BeginTransaction())
{
context.Script("update Product set Name = @0 where ProductId = @1")
.Parameters("The Warren Buffet Way", 1)
.Execute();
context.Update("Product").Column("Name", "The Warren Buffet Way")
.Where("ProductId", 1)
.Execute();
context.Update<Product>("Product", product)
.AutoMap(x => x.ProductId)
.Where(x => x.ProductId)
.Execute();
context.Commit();
}
平台规范:
在本平台新增、更新、删除数据均采用以上API完成。由于数据查询的复杂性和可变性,平台规范要求使用SQL语句,示例如下:
首先在脚本管理器中定义查询脚本,例如”2001”号脚本
API:
Product product = DataContext.ScriptById(2001).QuerySingle<Product>();
或
List<Product> products = DataContext.ScriptById(2001).QueryMany<Product>();
或
Product product = DataContext.ScriptById(2001)
.Parameter("ProductId", 3).QuerySingle<Product>();