Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 3e1ffa5

Browse files
committed
Add Stored Procedure example
1 parent fbc827f commit 3e1ffa5

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

README.md

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -986,13 +986,37 @@ typedRow = db.SingleById<Target>(1); //= null
986986

987987
## T4 Template Support
988988

989-
[Guru Kathiresan](https://github.com/gkathire) continues to enhance [OrmLite's T4 Template support](https://github.com/ServiceStack/ServiceStack.OrmLite/tree/master/src/T4) which are useful when you want to automatically generate POCO's and strong-typed wrappers for executing stored procedures. OrmLite's T4 support can be added via NuGet with:
989+
[OrmLite's T4 Template](https://github.com/ServiceStack/ServiceStack.OrmLite/tree/master/src/T4)
990+
are useful in database-first development or when wanting to use OrmLite with an existing
991+
RDBMS by automatically generating POCO's and strong-typed wrappers
992+
for executing stored procedures.
993+
994+
OrmLite's T4 support can be added via NuGet with:
990995

991996
PM> Install-Package ServiceStack.OrmLite.T4
992997

993-
## Custom SQL API's
998+
## Typed SqlExpressions with Custom SQL APIs
999+
1000+
The Custom SQL API's allow you to map custom SqlExpressions into different responses:
1001+
1002+
```csharp
1003+
List<Person> results = db.SqlList<Person>(db.From<Person>().Select("*").Where(q => q.Age < 50));
1004+
List<Person> results = db.SqlList<Person>("SELECT * FROM Person WHERE Age < @age", new { age=50});
1005+
1006+
List<string> results = db.SqlColumn<string>(db.From<Person>().Select(x => x.LastName));
1007+
List<string> results = db.SqlColumn<string>("SELECT LastName FROM Person");
1008+
1009+
HashSet<int> results = db.ColumnDistinct<int>(db.From<Person>().Select(x => x.Age));
1010+
HashSet<int> results = db.ColumnDistinct<int>("SELECT Age FROM Person");
1011+
1012+
int result = db.SqlScalar<int>(db.From<Person>().Select(Sql.Count("*")).Where(q => q.Age < 50));
1013+
int result = db.SqlScalar<int>("SELCT COUNT(*) FROM Person WHERE Age < 50");
1014+
```
1015+
1016+
## Stored Procedures using Custom Raw SQL API's
9941017

995-
Custom SQL API's provide a convenient way for executing custom sql and mapping to , e.g:
1018+
The Raw SQL API's provide a convenient way for mapping results of any Custom SQL like
1019+
executing Stored Procedures:
9961020

9971021
```csharp
9981022
List<Poco> results = db.SqlList<Poco>("EXEC GetAnalyticsForWeek 1");
@@ -1004,26 +1028,44 @@ List<int> results = db.SqlList<int>("EXEC GetTotalsForWeek @weekNo", new { weekN
10041028
int result = db.SqlScalar<int>("SELECT 10");
10051029
```
10061030

1007-
More examples can be found in [SqlServerProviderTests](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/SqlServerProviderTests.cs).
1008-
1009-
### Using typed SqlExpression in Custom SQL APIs
1031+
### Stored Procedures with output params
10101032

1011-
The Custom SQL API's also allow querying with Typed SQL Expressions:
1033+
The `SqlProc` API provides even greater customization by letting you modify the underlying
1034+
ADO.NET Stored Procedure call by returning a prepared `IDbCommand` allowing for
1035+
advanced customization like setting and retriving OUT parameters, e.g:
10121036

10131037
```csharp
1014-
List<Person> results = db.SqlList<Person>(db.From<Person>().Select("*").Where(q => q.Age < 50));
1015-
List<Person> results = db.SqlList<Person>("SELECT * FROM Person WHERE Age < @age", new { age=50});
1038+
string spSql = @"DROP PROCEDURE IF EXISTS spSearchLetters;
1039+
CREATE PROCEDURE spSearchLetters (IN pLetter varchar(10), OUT pTotal int)
1040+
BEGIN
1041+
SELECT COUNT(*) FROM LetterFrequency WHERE Letter = pLetter INTO pTotal;
1042+
SELECT * FROM LetterFrequency WHERE Letter = pLetter;
1043+
END";
10161044

1017-
List<string> results = db.SqlColumn<string>(db.From<Person>().Select(x => x.LastName));
1018-
List<string> results = db.SqlColumn<string>("SELECT LastName FROM Person");
1045+
db.ExecuteSql(spSql);
10191046

1020-
HashSet<int> results = db.ColumnDistinct<int>(db.From<Person>().Select(x => x.Age));
1021-
HashSet<int> results = db.ColumnDistinct<int>("SELECT Age FROM Person");
1047+
var cmd = db.SqlProc("spSearchLetters", new { pLetter = "C" });
1048+
var pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output);
10221049

1023-
int result = db.SqlScalar<int>(db.From<Person>().Select(Sql.Count("*")).Where(q => q.Age < 50));
1024-
int result = db.SqlScalar<int>("SELCT COUNT(*) FROM Person WHERE Age < 50");
1050+
var results = cmd.ConvertToList<LetterFrequency>();
1051+
var total = pTotal.Value;
1052+
```
1053+
1054+
An alternative approach is to use `SqlList` which lets you use a filter to customize a
1055+
Stored Procedure or any other command type, e.g:
1056+
1057+
```csharp
1058+
IDbDataParameter pTotal = null;
1059+
var results = db.SqlList<LetterFrequency>("spSearchLetters", cmd => {
1060+
cmd.CommandType = CommandType.StoredProcedure;
1061+
cmd.AddParam("pLetter", "C");
1062+
pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output);
1063+
});
1064+
var total = pTotal.Value;
10251065
```
10261066

1067+
More examples can be found in [SqlServerProviderTests](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/SqlServerProviderTests.cs).
1068+
10271069
## New Foreign Key attribute for referential actions on Update/Deletes
10281070

10291071
Creating a foreign key in OrmLite can be done by adding `[References(typeof(ForeignKeyTable))]` on the relation property,

0 commit comments

Comments
 (0)