SqlQuery return null every time - c#

I'm trying to execute a store procedure like this:
public bool Test(){
var searchItem=base.Database.SqlQuery<QueryEntity>("exec nameOfMyProcedure #param1={0}",param13).FirstOrDefault();
if(searchItem!=null){
return searchItem.Output1;
}
else{
return false;
}
private class QueryEntity{
public bool Output1{get;set;}
}
I've one Return in the stored procedure
Unfortunately, searchItem always returns null.
Have you an idea how I can resolve that?
I'm using SQL server and C#.

Your stored procedure is returning more that one columns. That can be the reason for the error you are getting.

You can try something like this
base.Database.SqlQuery<IEnumerable<string>>("exec nameOfMyProcedure #param1={0},#param2={1},#param3={2}",param1,param2,param3)
basically this is an example and you need to do is, look into the structure of what your stored procedure returns and then use suitable object for parsing.
Here i am assuming all the returns values are string.

Find the solution.
It's not really proper but.. It's work
I create a new stored procedure which return 1 output like this
Select #Return_value as Return
instead of
Return #Return_value

Related

Why does calling a T-SQL function using Entity Framework Core always return -1?

I am using ASP.NET Core and C# if that matters. When I get to the following line in my controller:
var validLogin = context.Database.ExecuteSqlRaw($"SELECT * FROM dbo.[valid_login]('{username}','{password}')");
I will always get -1 returned. I also used ExecuteSqlCommand and got the same result. I have tried creating both table defined and scalar functions and in either case it just returns -1.
The strangest part is that it works in SSMS. When I hit the breakpiont this is the query being sent:
{SELECT * FROM dbo.[valid_login]('test#email.com','password')}
When I run that in SSMS I get the correct result of 1. If it matters here is the latest version of the function. But like I said I get the exact same results with a scalar function.
ALTER FUNCTION valid_login
(#email varchar(1000),
#password varchar(1000))
RETURNS TABLE
AS
RETURN
(SELECT
CASE WHEN (SELECT COUNT(*) FROM [user]
WHERE #email = email_address
AND HASHBYTES('SHA2_512', CONCAT(password_salt, #password)) = password_hash) = 1
THEN 1
ELSE 0
END AS valid)
GO
Clearly, one of two things must be happening. Either Entity Framework Core is changing my SQL code before running it, or I am not using the correct method to get a result from a function. I don't think it's the second one as I do not see a simple context.Database.RunSqlFunction() method or anything like it. And if it's the first one I am not knowledgeable enough to know why that is happening.
Really, I just need to know how to get the result of a T-SQL function in ASP.NET Core using Entity Framework Core. So if you can let me know the simplest way to do that, or what modifications I need to make to make this work, that would be appreciated.
You always get 1 , because it is the number of selected rows.
If you want to get some data from DB you have to use this algorithm:
Create a special class to get data from sp. This class should have all properties that you select in your query string. You don't need to select everything. Just select what you need.
public class ResultData
{
public string Property1 {get;set;}
public string Property2 {get;set;}
.....
.....
}
Add to dbContext ResultData DbSet and config no key like this
public virtual DbSet<ResultData> ResultData { get; set; }
....
modelBuilder.Entity<ResultData>(e =>
{
e.HasNoKey();
});
And this a sample function to get data
public async Task<IEnumerable<ResultData>> GetData(int id, string name)
{
return await _context.Set<ResultData>()
.FromSqlInterpolated($"SELECT Property1, Property2 FROM dbo.[valid_login]('{username}','{password}')")
.ToArrayAsync();
}
I highly recommend you to use parameters instead of string values.

Entity Framework Core using stored procedure to delete entity with bool return

I have an entity, lets just call it "Entity", that I want to delete with a stored procedure. The "Entity" entity is relatively complex with a lot of related entities - Hence why, I want to use a stored procedure to delete the Entity.
CREATE PROCEDURE dbo.spDeleteEntity
#EntityId int,
#ServiceResult bit output
AS
BEGIN
.... Delete logic here ...
IF ##ERROR = 0
SET #ServiceResult = 1
ELSE SET #ServiceResult = 0
END
As you can see, the stored procedure takes in an EntityId for the entity, performs my delete logic, and returns a bit - Which in this case is my ServiceResult. Here the ServiceResult is "True"/1 if no errors occur while executing the query, and "False"/0 if errors occur. The problem is now, that I want to be able to execute this stored procedure from .NET Core. My Initial idea was to do something like this
public bool DeleteEntity(Entity Entity)
{
return _context.Entity.FromSqlRaw<bool>("spDeleteEntity {0}", Entity.Id);
}
I believe this doesn't work, because Entity Framework Core does not know what datatype it should expect. From what I can read, Entity Framework Core only accepts types of TEntity. So my question really is, how do I call a stored procedure with Entity Framework Core, so that I can pass an Id and get a bool value returned.
While in your case, you could simply RAISERROR in your procedure to indicate failure.;
try{
_context.Database.ExecuteSqlInterpolatedAsync($"spDeleteEntity {Entity.Id}");
return true;
}catch(...){
return false;
}
There is a way to pass sql parameters in / out of raw sql commands using EF Core with something like;
var entityId = new SqlParameter("#entityId", Entity.Id);
var result = new SqlParameter("#result", SqlDbType.Bit)
{ Direction = ParameterDirection.Output };
_context.Database.ExecuteSqlRaw("EXEC #result = spDeleteEntity #entityId", entityId, result);
return (bool)result.Value;
Call your stored procedure in the try catch and add your SqlParameter that you want to pass to sp like this :
try
{
using(var context = new SampleContext())
{
//Declare storedprocedure parameter
var Idp = new SqlParameter("#IdParam", "Idp");
//Call stored procedure(For async call use ExecuteSqlCommandAsync method)
context.Database.ExecuteSqlCommand("EXEC spName #IdParam", Idp);
}
Return true:
}
catch
{
Return false;
}
In this way if execution of sp occurred with error, it goes to catch and return false and if not it's return true.
Note: if you declare raiseerror in your storedprocedure, you can generates an error message and send it to your application try catch.
More details about raiseerror :
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql?view=sql-server-ver15

Calling Stored Procedure In Entity Framework

I have the following part in the end of a SQL Server stored procedure:
if(#someValue < 0)
begin
SELECT #resultIsSuccess = 0
Return #resultIsSuccess
end
else
begin
SELECT #resultIsSuccess = 1
Return #resultIsSuccess
end
where #resultIsSuccess is of type bit.
So, basically I am returning a bool to indicate if the procedure yielded the intended result.
On the EF side, I configured the Function Import's return type as boolean.
When I call:
bool isSuccess = context.MyFunctionImport(arg1, arg2).FirstOrDefault().Value;
I get the following exception:
The data reader returned by the store data provider does not have
enough columns for the query requested.
What is the problem here?
If you are returning a value, you need a return parameter. Therefore you aren't using a datareader to get your value. You aren't selecting anything therefore you would need to have access to that return parameter.
Instead select your values to populate the datareader since you are probably getting an error when you try to read values from a noninstantiated datareader through EF.
if(#someValue < 0)
SELECT 0
else
SELECT 1
I'm unsure if return values are supported, reference, so you may be trying to do the impossible unless fixed in a newer version.
You have to keep in mind that you are NOT returning a bit datatype from your procedure. The return datatype of a stored procedure is int.
You could greatly simplify your code to a single line return statement.
return case when #someValue < 0 then 0 else 1 end
Then in your code you would need to parse the 0 or 1 to a boolean.
--EDIT--
Since you are looking for the first value you will need to use a select statement in your procedure. Something like this.
Select isSuccess = case when #someValue < 0 then 0 else 1 end

Trying to checking if some user exists in my database

I'm trying to check if some user exists in my database or not using this stored procedure:
ALTER PROCEDURE see_if_writer_exsists_or_not
#username nvarchar(50),
#password nvarchar(50)
AS
select count(*)
from writers
where username = #username and password = #password
RETURN
Then I invoke it as a method using Linq-to-SQL with this code:
int b = DS.see_if_writer_exsists_or_not(username.Text, password.Text);
if (b > 0)
{ // the rest of the code...
but a conversion error appears
Cannot implicitly convert type'System.Data.Linq.ISingleResult' to 'int'
How to fix this ?
Note: I've tried Convert.ToInt32 method and it didn't work!
there is no problem with the stored procedure! if u want to check about if this username and password exists
just check if your method returns values or not by using .count()
if (DS.see_if_writer_exsists_or_not(username.Text, password.Text).count() > 0)
{
// yourcode...
}
I have found a solution for this also to extract my retrieved record from the database by using .ElementAt(0)method ,and here is the update in my code :
DS.see_if_writer_exsists_or_not(username.Text, password.Text).ElementAt(0)
by using this I could get the first element retrieved and that's what I wanted
It's just like the error message says:
Your call to see_if_writer_exsists_or_not returns an ISingleResult instance.
A call of ToString() returns "System.Data.Linq.ISingleResult" (which of course cannot be cast to an int).
You must return the query result from your stored proc and regenerate the see_if_writer_exsists_or_not method to accomplish what you want.
ISingleResult is an enumerable. You can loop through it using foreach. You can also do something like this:
ISingleResult<int> results = YourMethodCall();
int returnValue;
if(results.Count()>0){
returnValue = results.First<int>();
}
Also, please remove ToString call from your method call.

Assign a stored procedure result as a bool variable on C#

Hello Is it possible to store the result of a stored procedure as a true/false value in a bool variable? Something like:
1)
bool var = ExecuteScalar("sp_name", parameters);
or
2)
bool var = ClassName.getValue(parameters);
where
getValue() is a method that has this code:
return dataBase.ExecuteDataSet("sp_name", parameters);
I want to know if a customer has a card or not. I was thinking of using what I have right now which is basically the second option above, and then create a condition whether the row count is more than zero. But I also want to know if there are any other methods I can use.
Thanks in advance.
You can simply do:
DataSet ds = dataBase.ExecuteDataSet("sp_name", parameters);
return ds.Tables[0].Rows.Count>0; //true if record found; false if no rows
Assuming your proc is simply doing a select using the parameters in the where clause. Something like:
select col1 from table a where condition=#parameter
You don't say which DBMS you are using. SQL Server, for example, does not have a bool data type, but you can make your sproc return an int, which you can test for nonzero, e.g.
bool result = ((int) ExecuteScalar("sp_name", parameters)) != 0;
NOTE: I have not tested this, but you could try making the sproc return a bit instead. This is still an integer value as far as SQL Server is concerned, but ADO.NET should detect it and automatically cast it to bool for you. It certainly does for sproc parameters.
ExecuteScalar returns an Object. So no, #1 above would throw an error. Assuming your stored procedure returns a value that can be converted to true/false, you'd have to do something like this:
bool var;
bool.TryParse(ExecuteScalar("sp_name", paramaters).ToString(), out var);

Categories

Resources