.Net平臺下相容.NET Standard 2.0,一個實現以Lambda表示式轉轉換標準SQL語句,使用強型別運算元據的輕量級ORM工具,在減少魔法字串同時,通過靈活的Lambda表示式組合,實現業務資料查詢的多樣性。
一、儲存過程資料獲取
1、儲存過程
Create PROCEDURE [dbo].[CustOrderHist] @CustomerID nchar(5)
AS
SELECT ProductName, Total=SUM(Quantity)
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE C.CustomerID = @CustomerID
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID
GROUP BY ProductName
2、例項一個執行器
string cnstr = “Data Source=(local);Initial Catalog=Northwind;User ID=test;Password=test”;
ConnectionStringConnectionProvider connectionProvider = new ConnectionStringConnectionProvider(cnstr);
IStatementExecutor target = new StatementExecutor(new SqlLogger(new List<ISqlLogWriter>() { new NoOpSqlLogger() }), connectionProvider);
3、引數定義
var paramDef = new ParameterDefinition[]
{
new ParameterDefinition
{
Name = “CustomerID”,
Value = “ALFKI”
}
};
4、呼叫ExecuteStoredProcedure獲取一個IDataReader
IDataReader dataReader = target.ExecuteStoredProcedure(“CustOrderHist”, paramDef);
while (dataReader.Read())
{
Console.WriteLine($”ProductName: {dataReader[“ProductName”]},Total: {dataReader[“Total”]}”);
}
二、InputOutput、OutPut引數
1、儲存過程
ALTER PROCEDURE [dbo].[MyTestOutParam]
( @testint int output,@teststr nvarchar(50) output )
AS
BEGIN
SELECT @testint=100+@testint, @teststr=`test out put 測試`
END
2、引數定義
var paramDef2 = new ParameterDefinition[]
{ new ParameterDefinition
{ Name = “@testint”,
DbType=DbType.Int32,
Direction=ParameterDirection.InputOutput,
Value=99, },
new ParameterDefinition
{ Name = “@teststr”,
DbType=DbType.String,
Direction=ParameterDirection.Output,
Size=100 }
};
3、呼叫ExecuteStoredProcedure
var dataReader2 = target.ExecuteStoredProcedure(“MyTestOutParam”, paramDef2);
4、呼叫GetParameterCollection方法獲取返回值
dataReader2.GetParameterCollection(paramDef2);