How to execute a sql string in the application layer
See original GitHub issueI have a very complex business logic,it’s sql string is
SELECT * FROM
(SELECT OperatorID,Name,YEAR(OrderDate) AS ye,SUM(PayFee) AS scanCode FROM dbo.Operators JOIN dbo.Orders ON dbo.Operators.Id=dbo.Orders.OperatorID GROUP BY OperatorID,name,YEAR(OrderDate)
) AS a
JOIN (SELECT OperatorID,SUM(TheDayMoney)AS coinsCast,Year FROM dbo.DeviceCoinsRecords GROUP BY OperatorID,Year
)AS b ON a.OperatorID=b.OperatorID AND a.ye=b.Year WHERE b.Year=YEAR(GETDATE())
How can I do this?
Issue Analytics
- State:
- Created 8 years ago
- Comments:23 (12 by maintainers)
Top Results From Across the Web
sp_executesql (Transact-SQL) - SQL Server
With EXECUTE, each INSERT string is unique because the parameter values are different. Although both methods generate the same number of batches ...
Read more >Execute Dynamic SQL commands in SQL Server
SQL Server offers a few ways of running a dynamically built SQL statement. Here are a few options: Writing a SELECT statement or...
Read more >Dynamic SQL in SQL Server
In this article, we will review how to construct and execute dynamic SQL statements in SQL Server with different examples.
Read more >How to execute an .SQL script file using c# - ...
Put the command to execute the sql script into a batch file then run the below code string batchFileName = @"c:\batosql.bat"; ...
Read more >SQL Layer
The SQL layer of CockroachDB's architecture exposes its SQL API to developers and converts SQL statements into key-value operations.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi,
You don’t have to use repositories. Repository is not a good point for such a generic code. Define an interface like this, in your .Core module:
Then implement it in your .EntityFramework module:
Then you can inject ISqlExecuter anywhere you need. And you can use it like that:
For the second question, if you use EF’s class, you need to reference to EF, no way. Why don’t you return list of entities if you need to run sql and get a list of entities.
@acjh @hikalkan thank you both for the help, I was able to get typed stored proc calls working. Here’s the code:
Interface (in domain project):
Implementation (in EntityFramework project):
Then you can inject the repository like any other in the application service layer, providing the type that the Stored Proc will return:
Then to call the stored proc from the app service, you have to wrap it in a unit of work transaction, otherwise the datareader is not freed correctly:
Hopefully that helps for anyone else that comes across this issue.