Using LINQ to SQL or Entity Framework really gives you good, strongly typed access to your parameters and results, but there are a lot of considerations to make before using any of them. LINQ to SQL is a lifetime, and the Entity Framework will overhaul the .NET Framework 4.0.
To make a conscious consideration, I thought that I would use the ADO.NET low-development method:
void CallMyStoredProcedure(string connectionString, string paramValue) { string spName = "MyStoredProcedure"; string columnValue; using (SqlConnection conn = new SqlConnection(connectionString)) using (SqlCommand cmd = new SqlCommand(spName, conn)) { cmd.CommandType = CommandType.StoredProcedure; // Set parameter values cmd.Parameters.AddWithValue("@inputParam", (object)paramValue ?? DBNull.Value); // Parameter to store return value of your SP SqlParameter returnParam = cmd.Parameters.Add( new SqlParameter("RETURN_VALUE", SqlDbType.Int)); returnParam.Direction = ParameterDirection.ReturnValue; // Execute SP and read results conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { // Do something with return value int? returnValue = cmd.Parameters["RETURN_VALUE"].Value as int?; while (dr.Read()) { // Suggested way to read results if you use // "SELECT * FROM" in your SP. If you return columns // in a fixed order, it fairly safe to be reading // in that order without looping for (int field = 0; field < dr.FieldCount; field++) { switch (dr.GetName(field)) { case "StringColumn": columnValue = dr.GetValue(field) as string; break; default: // Column not recognized by your code. // You might choose to complain about it here. break; } } } } } }
Yes, this is very little code, so you might want to write some helper methods if you want to switch to vanilla ADO.NET.
source share