C# : how to use parameters for SQL Server stored procedure?

Viewed 1877

I want to use stored procedures in C# to get data from a SQL Server table. I want to print the city with the id that I'll pass as a parameter, but I don't know the correct syntax of using parameters like that for a stored procedure in C#.

This is the C# code that I'm using:

string connString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    SqlCommand command = new SqlCommand("SP_GetCityByID where id = 2", conn);
    command.CommandType = CommandType.StoredProcedure;

    SqlParameter param = command.Parameters.Add("@ID", SqlDbType.Int);
    param.Direction = ParameterDirection.Input;

    command.ExecuteNonQuery();

    Console.WriteLine(param.Value);
}
2 Answers
Related