c# MySql.Data.EntityFrameworkCore store procedure pagination - c#

for using pagination in complex query before i used sql server (tsql) i can do it very ease using COUNT(*) OVER() as TotalCount in any complex query it will return total count and i can use it in pagination but in mysql it will not work
to connect with mysql with asp.net core there is two packages
MySql.Data.EntityFrameworkCore
Pomelo.EntityFrameworkCore.MySql
i used both but i don't know how to implement pagination in store procedure and return using FromSqlRaw the issue is return number of rows OUT TotalCount INT that i can't get it
DELIMITER $$
CREATE PROCEDURE get_articles(IN _offset INT, IN _count INT, OUT _total INT)
BEGIN
SELECT SQL_CALC_FOUND_ROWS *
FROM content c JOIN content_types t
ON c.content_type = t.id
WHERE t.name = 'article'
LIMIT _offset, _count;
SET _total = FOUND_ROWS();
END$$
DELIMITER ;
c# with MySql.Data.EntityFrameworkCore
var result = await dbContext.Result.FromSqlRaw("CALL get_articles (0,30,#TotalCount)",totalCount
).AsNoTracking().ToListAsync();
Here is SQLFiddle demo

Your procedure should accpet first element or page number then you can combine limit keyword in your SQL. For example limit kpagecountpagenumber , kpagecount(pagenumber+1)-1
There is also version which takes limit n offset m
After edit:
please include also parameters in your call (C#).

Related

C# - SQL - add rowsCount dynamically to data models

I have a code where i do sql query by casting the table model like this:
string sql = string.Format("SELECT * FROM {0}...", tableName...);
and then:
IEnumerable<T> r = dbConn.Connection.Query<T>(sql...);
the thing is if i want to get total rowsCount(of course i can get count on the "r" but if there is a where clause its not possible because i want total count) i have to another query without where.
so i want to remove the second query. i did this in sql query to get rowsCount:
string sql = string.Format("SELECT *, count(*) over() rowsCount FROM {0}...", tableName...);
i can get the rowsCount with this query but since neither one of models has rowsCount i cant access it, is there any suggestions on how i should do it?
Edit:
first query has paging filter by using offset and limit, so i want totalcount not the count of filtered query.
I'm looking to see if there is a way to not use two seperate queries, and get results and also rowsCount by just one query.
You will have to do 2 SQL queries.
Nothing stopping you running them in one SQL block or calling a stored proc with output parameters. So you don't have to make 2 calls but you will need 2 queries at least.
https://www.sqlservertutorial.net/sql-server-stored-procedures/stored-procedure-output-parameters/
If you are worried about performance of a total count just make sure you have an index on the smallest column in that table and it should be mega fast.
The below example return a dataset and an output in one call
DROP PROCEDURE IF EXISTS dbo.DataSetAndOutput
GO
CREATE PROCEDURE dbo.DataSetAndOutput
#YourId BIGINT,
#CountRecords INT OUTPUT
AS
BEGIN
SELECT * FROM YourTable
WHERE Id = #YourId
SET #CountRecords = (SELECT COUNT(YourId) FROM YourTable)
END
GO
-- Test the output
DECLARE #ResultCount INT
EXEC dbo.DataSetAndOutput #YourId= 252452, #CountRecords = #ResultCount OUTPUT
SELECT #ResultCount AS TheCount

SQL INSERT and SELECT into Oracle using a single command with .NET

I'm trying to execute a SQL command that insert a record on a table and returns the generate primary key.
I'm using .NET Core 3.1 and Oracle.ManagedDataAccess.Core package.
This is the C# code to execute the SQL command (it uses some extension methods but is clear how it works):
private int PutSomethingInTheDatabase(string entity)
{
string sqlComamnd = File.ReadAllText("SQL//Insert Card.sql");
using (var connection = new Oracle.ManagedDataAccess.Client.OracleConnection(connectionString))
using (var command = connection.OpenAndUse().CreateTextCommand(sqlComamnd))
{
//var reader = command.ExecuteReader();
//reader.Close();
//var result = command.ExecuteScalar();
//return (int)(decimal)result;
return -1;
}
}
Ideally I will receive a single value and read it with ExecuteScalar().
It is an itegration test (thats why I read the SQL from a file).
The SQL I want to use should INSERT the new record and return the generated sequence within the same scope/transaction, that's whi I'm using Begin/End but I'm not sure it is the right way.
My problem is that I cannot find the right syntax to execute the last SELECT to return the generated sequence_id, I also tried with RETURN...
This is the SQL:
declare new_id number;
BEGIN
select seq_stage_card.NEXTVAL into new_id from dual;
INSERT INTO spin_d.stage_card (
sequence_id,
field_1,
field_2
)
VALUES (
new_id,
'aaa'
TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')
);
select new_id from dual where 1 = 1 ; -- not valid
END;
-- return new_id ; -- not valid
-- select new_id from dual ; -- not valid
How to change the SQL in order to return the new_id ?
There is another (better) way to achieve the same result?
Is it safe (isolated scope), or the select will return a wrong ID if there is a concurrent insert?
[Update]
Someone suggested to use RETURNING (see here: Oracle - return newly inserted key value)
I already tried to use RETURN and RETURNING but I haven't find any real example of usage with the .NET (or other frameworks) driver, eg. OracleSqlCommand and the right call to execute.
Maybe it works but I still cannot figure out how to use it.
In general case (when you have to implement some logics within anonymous block, and when returning is not an option) try bind variables: first, turn new_id into :new_id in the query:
BEGIN
SELECT seq_stage_card.NEXTVAL
INTO :new_id -- bind variable to return back to c#
FROM dual;
INSERT INTO spin_d.Stage_Card (
sequence_id,
field_1,
field_2
)
VALUES (
:new_id,
'aaa',
TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')
);
END;
Then use it in C# code:
...
using (var command = connection.OpenAndUse().CreateTextCommand(sqlComamnd))
{
//TODO: check the syntax and RDBMS type
command.Parameters.Add(
":new_id",
OracleDbType.Int32).Direction = ParameterDirection.Output;
// Execute query
command.ExecuteNonQuery();
// Bind variable reading
return Convert.ToInt32(command.Parameters[0].Value);
}

Entity Framework doesn't support stored procedures which build result sets from Dynamic queries or Temporary tables

I've written the following stored proc, which works fine. What I want to do with it though is use it an entity data model. However using it in the entity data model maps to a return type of integer, and a value of zero.
How do I get the SP to return the actual data instead of an integer using the DataContext ?
IF EXISTS (SELECT * FROM SYS.OBJECTS WHERE TYPE = 'P' AND NAME = 'myProc') DROP PROCEDURE myProc;
GO
CREATE PROCEDURE [dbo].myProc
#START DateTime, #STOP DateTime
AS
BEGIN TRY
CREATE TABLE #Temp (download_Pk int);
INSERT INTO #Temp
SELECT download_pk FROM t1
UNION ALL
SELECT download_pk FROM t2;
WITH
x as
(
SELECT ID as Caps_Pk,
rootID as [Caps_RootId],
Case400Series as [Case],
SUBSTRING(c1, CHARINDEX('_', c1,1)+1, LEN(c1)) as [Customer],
run as Run,
SUBSTRING(c2, 1, CHARINDEX('_', c2, 1) -1) as [Sample],
SUBSTRING(c2, CHARINDEX('_', c2,1)+1, len(c2)) as [Amplification],
projectTitle,
DateAdded as [UploadTime],
UserId as [User]
FROM t3
WHERE DateAdded >= #START AND DateAdded <= #STOP AND
[User] in (SELECT name FROM ViewUsers WHERE Site = 'abc' AND Role = 'def')
)
SELECT *
FROM x
WHERE Caps_Pk NOT IN (Select download_Pk from #Temp)
DROP TABLE #Temp;
END TRY
BEGIN CATCH
DROP TABLE #Temp;
END CATCH
GO
Thanks in Advance.
Have you looked at this Code First Stored Procudure, this works great for me, it also has a NuGet Pckage that you can install.
found the answer here: EF4 - The selected stored procedure returns no columns
"EF doesn't support importing stored procedures which build result set from:
Dynamic queries
Temporary tables
The reason is that to import the procedure EF must execute it."

Why is Entity Framework calling my stored procedure but returning an incorrect value?

I have a stored procedure that simply returns the total number of records divided by whatever value is passed in. This is to aid in pagination on a website.
However, I am using the entity framework to bind to that stored procedure and it's returning -1 for all calls to it. When I interrogate the stored procedure using SQL Management Studio, it comes back with the correct value.
My stored procedure looks like this:
CREATE PROCEDURE [dbo].[GetAuditRecordPageCount]
#Count INTEGER
AS
RETURN ((SELECT COUNT(Id) FROM AuditRecords) / #Count) + 1
And my call to the entity framework in C# is this:
int pageCount;
using (Entities entities = new Entities())
{
pageCount = entities.GetAuditRecordPageCount(count);
}
Am I correct in writing the C# code this way?
As per a request in the comments, the SQL generated by EF is:
exec [dbo].[GetAuditRecordPageCount] #Count=100
Did you tried that?
http://www.devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values
I think your procedure will look like this:
CREATE PROCEDURE [dbo].[GetAuditRecordPageCount]
#Count INTEGER
AS
declare #retVal int
set #retVal = ((SELECT COUNT(Id) FROM AuditRecords) / #Count) + 1
select #retVal
And in the c# code:
int? pageCount;
using (Entities entities = new Entities())
{
pageCount = entities.GetAuditRecordPageCount(count).SingleOrDefault();
if (pageCount.HasValue)
{
//do something here
}
else
{
}
}
Don't forget to put "Scalars: Int32" in edit function Import screen.

SQL Server 2005: dynamically adding parameters to a stored procedure

Scenario
I have a stored procedure that takes a single parameter. I want to update this stored procedure to take a VARIABLE NUMBER OF PARAMETERS - a number that I will never know.
I currently use SQLConnections through a C# interface in order to pass in a single parameter to the stored procedure and return a result.
The SQL Part
Lets say that I have a stored procedure that returns a list of results based on a single input parameter "#ccy" - (Currency). Now lets say that I want to update this stored procedure to take a list of Currencies instead of a single one, but that this number will be variable depending on the situation.
The SQL Code
ALTER PROCEDURE [dbo].[SEL_BootStrapperInstRICs]
(
#ccy varchar(10)
)
AS
SELECT DISTINCT i.CCY, i.Instrument, i.Tenor, r.RIC, r.[Server], r.RIType
FROM MDR.dbo.tblBootStrapperInstruments as i, MDR.dbo.tblBootStrapperRICs as r
WHERE i.Instrument = r.MurexInstrument
AND
i.Tenor = r.Tenor
AND i.CCY = r.CCY
AND i.CCY = #ccy
AND r.RIType NOT LIKE '%forward%'
The C# Part
This particular stored procedure is called from a C# WinForms application that uses the "SqlCommand.Parameters.AddWithValue()" method. As mentioned earlier this method currently passes in a single Currency as the parameter to the stored procedure and returns the result as a DataSet.
public DataSet GetBootStrapperInstRICsDS(List<string> ccys)
{
DataSet ds;
SqlConnection dbConn = null;
SqlCommand dbCmd = new SqlCommand();
try
{
dbConn = GetSQLConnection();
dbCmd = GetSqlCommand();
dbCmd.CommandType = CommandType.StoredProcedure;
dbCmd.CommandText = Utils.Instance.GetSetting ("SELBootStrapInsRics", "default");
foreach(string ccy in ccys)
dbCmd.Parameters.AddWithValue("#ccy", ccy);
dbCmd.CommandTimeout = 600;
dbCmd.Connection = dbConn;
SqlDataAdapter adapter = new SqlDataAdapter(dbCmd);
ds = new DataSet();
adapter.Fill(ds, "tblBootStrapperInstRICs");
dbCmd.Connection.Open();
return ds;
}
catch (Exception ex)
{
ApplicationException aex = new ApplicationException ("GetBootStrapperInstRICsDS", ex);
aex.Source = "Dal.GetBootStrapperInstRICsDS " + ex.Message;
MainForm.job.Log(aex.Source, Job.MessageType.Error);
Job.incurredErrors = true;
throw aex;
}
finally
{
if (dbCmd != null)
dbCmd.Dispose();
if (dbConn != null)
{
dbConn.Close();
dbConn.Dispose();
}
}
}
The Question
On the C# side I think my best option is to use a "foreach/for loop" in order to iterate through a list of parameters and dynamically add a new one to the SPROC. (I have already made this update in the C# code above).
HOWEVER - Is there some way that I can do this in the SQL Stored Procedure too? My thoughts are split with two potential options - Either create 20 or more parameters in the SPROC (each with the same name but with an incrementing number at the end e.g. - #ccy1,#ccy2 etc.) and use "for(int i=0;i
for(int i=0;i<NumberOfCurrenciesToAdd;i++)
dbCmd.Parameters.AddWithValue("#ccy"+i, currencyArray[i]);
Or the other option is to do something completely different and less rubbish and hack-esque. Help greatly appreciated.
EDIT - SQL Server 2005
EDIT2 - Must Use SPROCS - Company Specification Requirement.
You never specified SQL Server version, but for 2008 there are Table-Valued Parameters, which may help you:
Table-valued parameters are a new parameter type in SQL Server 2008. Table-valued parameters are declared by using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.
I worked for a company that had to do this. It is much easier to just pass an nvarchar that is really a list that is comma delimited and then parse it when you get into the stored proc and insert the values into a temp table. The other option would be to have an xml parameter in your proc. That should also work. This is all for SQL 2005. 2008 does give you the table variable and that would be your best option.
I would try to stay away from dynamically changing your stored proc because I think that would be hard to maintain. At any given time if you try to look at the proc it could be different. Also, what happens when 2 people are trying to use your site and hit that proc at the same moment? One person's session will be modifying the procedure and the others will try to do it. This could cause a lock on the stored proc or it could cause other issues. Regardless it would be pretty inefficient.
Here is another option - though I think Anton's answer is better. You can pass in a csv string as a single parameter. Use a user-defined function to convert the csv string into a table of values, which you can join in your query. There are several csv parsing functions listed on SO and other places (though, sorry, I can't come up with a link right now).
edit: here is another option. Pass in the same csv string, then generate the sql query as a string in the procedure, and execute the string. Use the csv in an 'in' clause :
where i.ccy in (1,2,3,4)
I would not try to change the stored procedure, but (since you are on SQL Server 2005 and don't have table variable parameters) just pass in a comma separated list of values and let the procedure split them apart. You can change your C# loop to just build a CSV string and once you create a SQL split procedure, use it like:
SELECT
*
FROM YourTable y
INNER JOIN dbo.yourSplitFunction(#Parameter) s ON y.ID=s.Value
I prefer the number table approach to split a string in TSQL
For this method to work, you need to do this one time table setup:
SELECT TOP 10000 IDENTITY(int,1,1) AS Number
INTO Numbers
FROM sys.objects s1
CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
Once the Numbers table is set up, create this split function:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
#SplitOn char(1) --REQUIRED, the character to split the #List string on
,#List varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN
(
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(#SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT #SplitOn + #List + #SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = #SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
);
GO
You can now easily split a CSV string into a table and join on it:
select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
OUTPUT:
ListValue
-----------------------
1
2
3
4
5
6777
(6 row(s) affected)
Your can pass in a CSV string into a procedure and process only rows for the given IDs:
SELECT
y.*
FROM YourTable y
INNER JOIN dbo.FN_ListToTable(',',#GivenCSV) s ON y.ID=s.ListValue
I use this function to split CSV text into a table of numbers, it has great performance due to various optimizations (like returning a table with a primary key which greatly influence the query optimizer to produce good query plans ever for extremely large data sets).
Also it's not limited to 4000 characters, so you can pass in very large strings.
CREATE Function [dbo].[TextSplitToInt](#list text,
#delim char(1) = N',')
RETURNS #T TABLE (ID_T int primary key)
BEGIN
DECLARE #slices TABLE (slice nvarchar(4000) NOT NULL)
DECLARE #slice nvarchar(4000),
#textpos int,
#maxlen int,
#stoppos int
SELECT #textpos = 1, #maxlen = 4000 - 2
WHILE datalength(#list) / 2 - (#textpos - 1) >= #maxlen
BEGIN
SELECT #slice = substring(#list, #textpos, #maxlen)
SELECT #stoppos = #maxlen - charindex(#delim, reverse(#slice))
INSERT #slices (slice) VALUES (#delim + left(#slice, #stoppos) + #delim)
SELECT #textpos = #textpos - 1 + #stoppos + 2 -- On the other side of the comma.
END
INSERT #slices (slice)
VALUES (#delim + substring(#list, #textpos, #maxlen) + #delim)
INSERT #T (ID_T)
SELECT distinct Cast(str as int)
FROM (SELECT str = ltrim(rtrim(substring(s.slice, N.Number + 1,
charindex(#delim, s.slice, N.Number + 1) - N.Number - 1)))
FROM Numbers N
JOIN #slices s ON N.Number <= len(s.slice) - 1
AND substring(s.slice, N.Number, 1) = #delim) AS x
RETURN
END

Categories

Resources