DataAccess

Author: Victor.X.Qu

Email: [email protected]

DataAccess is a c# project for sql data mapping to object, like dapper

DataAccess is for net core , so it base on netstandard1.3

db supports

DataAccess base on ado.net, so you can use blow db :

use MSSql example

Use config file

dependencies
  "dependencies": {
"VIC.DataAccess.MSSql": "1.0.1",
"VIC.DataAccess.Config": "1.0.1"
},

You can config sql in xml file for DataAcces, like:

<?xml version="1.0" encoding="utf-8"?>
<DbConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ConnectionStrings>
<DataConnection Name="Test" ConnectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TestDataAccess;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />
</ConnectionStrings>
<SqlConfigs>
<DbSql CommandName="SelectByName" Type="Text" ConnectionName="Test">
<Text>
<![CDATA[
SELECT top 1
Id
,Age
,Name
,JoinDate
,[Money]
FROM [dbo].[Students] WITH(NOLOCK)
WHERE @Name = Name
]]>
</Text>
<PreParameters>
<Parameter Name="@Name" Direction="Input" Type="AnsiString" />
</PreParameters>
</DbSql>
<DbSql CommandName="SelectAll" Type="Text" ConnectionName="Test">
<Text>
<![CDATA[
SELECT
Id
,Age
,Name
,JoinDate
,[Money]
FROM [dbo].[Students] WITH(NOLOCK)
]]>
</Text>
</DbSql>
<DbSql CommandName="SelectAllAge" Type="Text" ConnectionName="Test">
<Text>
<![CDATA[
SELECT
sum(Age) as Age
FROM [dbo].[Students] WITH(NOLOCK)
]]>
</Text>
</DbSql>
<DbSql CommandName="Clear" Type="Text" ConnectionName="Test">
<Text>
<![CDATA[
delete from [dbo].[Students]
]]>
</Text>
</DbSql>
<DbSql CommandName="BulkCopy" Type="Text" ConnectionName="Test">
<Text>
<![CDATA[
[dbo].[Students]
]]>
</Text>
</DbSql>
</SqlConfigs>
</DbConfig>

Code for use :

var provider = new ServiceCollection()
.UseDataAccess()
.UseDataAccessConfig(Directory.GetCurrentDirectory(), false, "db.xml")
.BuildServiceProvider(); List<Student> students = GenerateStudents(count); var db = provider.GetService<IDbManager>(); var command = db.GetCommand("BulkCopy");
await command.ExecuteBulkCopyAsync(students); var command = db.GetCommand("SelectByName");
Student s = await command.ExecuteEntityAsync<Student>(new { Name = "3" }); var command = db.GetCommand("SelectAll");
List<Student> students = await command.ExecuteEntityListAsync<Student>(); var command = db.GetCommand("SelectAllAge");
int? age = await command.ExecuteScalarAsync<int?>();

No config file

dependencies
  "dependencies": {
"VIC.DataAccess.MSSql": "1.0.1"
},
var provider = new ServiceCollection()
.UseDataAccess()
.BuildServiceProvider(); var command = provider.GetService<IDataCommand>();
command.ConnectionString = "sqlConnectionString";
command.Text = "sql";
command.Type = CommandType.Text;
Student s = await command.ExecuteEntityAsync<Student>(new { Name = "3" });

Test performance

You can see the simple code in https://github.com/fs7744/DataAccess/blob/master/example/MSSqlExample

又一个类dapper轮子:VIC.DataAccess-LMLPHP

All package

05-11 15:11