Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error could not find stored procedure with Dapper and c# only when I specify commandType

Tags:

c#

dapper

I'm using Dapper with C# and SQL Server. I have an stored procedure in SQL Server that inserts a person into my database. The problem is that when I specify in the query that its an stored procedure I get

Could not find stored procedure

but without using commandType, everything works fine.

For example: I had the following code:

var result = connection.Execute("KBunnyGame_Players_InsertPlayer @Name, @Score, @FacebookId", player, commandType: CommandType.StoredProcedure);

And I kept getting the error, but after I changed to:

var result = connection.Execute("KBunnyGame_Players_InsertPlayer @Name, @Score, @FacebookId", player);

I didn't get the error anymore and the player got inserted in the database. I don't know if that's the expected behaviour, it seems weird to me. Is there any documentation I could follow?

EDIT:

Stored procedure code:

ALTER PROCEDURE [dbo].[KBunnyGame_Players_InsertPlayer] 
    @Name NCHAR(20),
    @Score INT,
    @FacebookId NCHAR(50)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO KBunnyGame_Players (name, score, facebookId) 
    VALUES (@Name, @Score, @FacebookId); 
END
like image 216
Teler Avatar asked Nov 09 '18 22:11

Teler


People also ask

Could not find stored procedure even though it is there?

This error states “Could not find stored procedure 'GO'“. It simply means that the SQL Server could found the stored procedure with the name “GO“. Now, the main reason behind this error could be the misuse of the “GO” statement.

How do I find stored procedures in SQL Server?

Using SQL Server Management Studio In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies.

How do I execute a stored procedure in SQL Server?

In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure.


1 Answers

Don't specify the parameters in the command-text when using CommandType.StoredProcedure; provide just the name - the parameters are passed by name already.

If you need to filter the properties on the object, use a projection:

var result = connection.Execute("KBunnyGame_Players_InsertPlayer",
    new { player.Name, player.Score, player.FacebookId },
    commandType: CommandType.StoredProcedure);

SQL Server has implicit EXEC if something looks sufficiently close enough to an EXEC, which is why the other version works. So if you prefer: use that. With or without a leading EXEC.

like image 173
Marc Gravell Avatar answered Oct 21 '22 15:10

Marc Gravell