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);
}
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!