A syntax error occurred while calling a stored procedure. I do not understand what the problem is.

I am unable to call the following stored procedure. When I call ExecuteReader, I get the error message β€œWrong syntax next toβ€œ GetAverages2. ”I can create a stored procedure and call it from TSQL. I cannot get it to work with ADO:

CREATE PROCEDURE GetAverages2
    @CompanySize INT,
    @Q1         FLOAT OUT,
    @Q2         FLOAT OUT
AS  
    SELECT @Q1 = 1, @Q2 = 2
GO

    string connectionString = ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand("GetAverages2", connection))
        {
            command.Parameters.Add("@CompanySize", System.Data.SqlDbType.Int).Value = int.Parse(Request.QueryString["CompanySizeId"]);
            command.Parameters.Add("@Q1", System.Data.SqlDbType.Float).Direction = System.Data.ParameterDirection.Output;
            command.Parameters.Add("@Q2", System.Data.SqlDbType.Float).Direction = System.Data.ParameterDirection.Output;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
+3
source share
1 answer

SqlCommand.CommandType defaults to CommandType.Text, so it tries to execute GetAverages2 as a raw SQL statement. Add this line immediately after creating the command:

command.CommandType = CommandType.StoredProcedure;
+2
source

Source: https://habr.com/ru/post/1717229/


All Articles