SQL stored procedure in .NET Core.

I call SQL stored procedures slightly differently in .NET Core to the way I did in .NET framework as I now pass the connection string through as an argument and I need to remember to add the System.Data.SqlClient NuGet package.

	
public static List<string> GetBlogSummaries(string connectionString)
{
	List<string> headers = new List<string>();

	using (SqlConnection connection = new SqlConnection(connectionString))
	{
		using (SqlCommand command = new SqlCommand("sp__get_required_headers"))
		{
			command.CommandType = System.Data.CommandType.StoredProcedure;
			command.Connection = connection;

			connection.Open();

			SqlDataReader reader = command.ExecuteReader();

			if (reader.HasRows)
			{
				while (reader.Read())
				{
					headers.Add(reader[0].ToString());
				}
			}

			reader.Close();
			connection.Close();
		}
	}

	return headers;
}
	

Leave a Reply

Your email address will not be published. Required fields are marked *