mssql helpers
1.0.0
MsSqlHelpers 是一個用於改進 MS SQL Server 常見開發任務的函式庫,例如產生與 ADO.NET、Entity Framework 和 Dapper 等(在不久的將來)一起使用的參數化批次插入。
為了允許 IDENTITY INSERT ON,方法呼叫中有一個可選參數(預設模式關閉)。
ADO.NET 用法:
using MsSqlHelpers ;
.. .
var mapper = new MapperBuilder < Person > ( )
. SetTableName ( "People" ) // could be ommited if your table's name is the same as you entity's class name
. AddMapping ( person => person . FirstName , columnName : "Name" )
. AddMapping ( person => person . LastName , columnName : "Surename" )
. AddMapping ( person => person . DateOfBirth ) // in this case property's name is the same as table column's name
. Build ( ) ;
var people = new List < Person > ( )
{
new Person ( ) { FirstName = "John" , LastName = "Lennon" , DateOfBirth = new DateTime ( 1940 , 10 , 9 ) } ,
new Person ( ) { FirstName = "Paul" , LastName = "McCartney" , DateOfBirth = new DateTime ( 1942 , 6 , 18 ) } ,
} ;
var connectionString = "Server=SERVER_ADDRESS;Database=DATABASE_NAME;User Id=USERNAME;Password=PASSWORD;" ;
var sqlQueriesAndParameters = new MsSqlQueryGenerator ( ) . GenerateParametrizedBulkInserts ( mapper , people ) ;
using ( var sqlConnection = new SqlConnection ( connectionString ) )
{
sqlConnection . Open ( ) ;
// Default batch size: 1000 rows or (2100-1) parameters per insert.
foreach ( var ( SqlQuery , SqlParameters ) in sqlQueriesAndParameters )
{
using ( SqlCommand sqlCommand = new SqlCommand ( SqlQuery , sqlConnection ) )
{
sqlCommand . Parameters . AddRange ( SqlParameters . ToArray ( ) ) ;
sqlCommand . ExecuteNonQuery ( ) ;
}
}
}
實體框架用法:
using MsSqlHelpers ;
.. .
var mapper = new MapperBuilder < Person > ( )
. SetTableName ( "People" ) // could be ommited if your table's name is the same as you entity's class name
. AddMapping ( person => person . FirstName , columnName : "Name" )
. AddMapping ( person => person . LastName , columnName : "Surename" )
. AddMapping ( person => person . DateOfBirth ) // in this case property's name is the same as table column's name
. Build ( ) ;
var people = new List < Person > ( )
{
new Person ( ) { FirstName = "John" , LastName = "Lennon" , DateOfBirth = new DateTime ( 1940 , 10 , 9 ) } ,
new Person ( ) { FirstName = "Paul" , LastName = "McCartney" , DateOfBirth = new DateTime ( 1942 , 6 , 18 ) } ,
} ;
var sqlQueriesAndParameters = new MsSqlQueryGenerator ( ) . GenerateParametrizedBulkInserts ( mapper , people ) ;
// Default batch size: 1000 rows or (2100-1) parameters per insert.
foreach ( var ( SqlQuery , SqlParameters ) in sqlQueriesAndParameters )
{
_context . Database . ExecuteSqlRaw ( SqlQuery , SqlParameters ) ;
// Depracated but still works: _context.Database.ExecuteSqlCommand(SqlQuery, SqlParameters);
}
精緻的用法:
using MsSqlHelpers ;
.. .
var mapper = new MapperBuilder < Person > ( )
. SetTableName ( "People" ) // could be ommited if your table's name is the same as you entity's class name
. AddMapping ( person => person . FirstName , columnName : "Name" )
. AddMapping ( person => person . LastName , columnName : "Surename" )
. AddMapping ( person => person . DateOfBirth ) // in this case property's name is the same as table column's name
. Build ( ) ;
var people = new List < Person > ( )
{
new Person ( ) { FirstName = "John" , LastName = "Lennon" , DateOfBirth = new DateTime ( 1940 , 10 , 9 ) } ,
new Person ( ) { FirstName = "Paul" , LastName = "McCartney" , DateOfBirth = new DateTime ( 1942 , 6 , 18 ) } ,
} ;
var connectionString = "Server=SERVER_ADDRESS;Database=DATABASE_NAME;User Id=USERNAME;Password=PASSWORD;" ;
var sqlQueriesAndDapperParameters = new MsSqlQueryGenerator ( ) . GenerateDapperParametrizedBulkInserts ( mapper , people ) ;
using ( var sqlConnection = new SqlConnection ( connectionString ) )
{
// Default batch size: 1000 rows or (2100-1) parameters per insert.
foreach ( var ( SqlQuery , DapperDynamicParameters ) in sqlQueriesAndDapperParameters )
{
sqlConnection . Execute ( SqlQuery , DapperDynamicParameters ) ;
}
}