kashmoney
kashmoney

Reputation: 442

Trouble using Dapper to call stored procedure from C#

I am completely new to C#, T-SQL, and Dapper and was trying to execute a stored procedure, but I am getting

System.Data.SqlClient.SqlException: Incorrect syntax around 'Josh'

My stored procedure is simply:

CREATE PROCEDURE [compName\Josh].[josh_test_insert]
    @ssn FLOAT,
    @gpa FLOAT
AS 
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [compName\Josh].josh_testing 
    VALUES (@ssn, @gpa);
END
GO

Where josh_testing has two fields of SSN and GPA.

The C# code is

public void InsertPerson(float ssn, float gpa)
{
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("SampleDB")))
    {
        List<new_app> apps = new List<new_app>();
        apps.Add(new new_app { ssn = ssn, gpa = gpa });
        connection.Execute("compName\\Josh.josh_test_insert @ssn, @gpa",apps);
    }
}

Anyone know what I'm missing here?

Upvotes: 2

Views: 487

Answers (1)

Alberto Chiesa
Alberto Chiesa

Reputation: 7350

Just escape the name of the sp as you would do in Management Studio:

public void InsertPerson(float ssn, float gpa)
{
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("SampleDB")))
    {
        connection.Execute("EXEC [compName\\Josh].[josh_test_insert] @ssn, @gpa", new { ssn = ssn, gpa = gpa });
    }
}

And pass the parameters without useless List<>s.

Upvotes: 3

Related Questions