// Execute the "GetCustomers" stored procedure and read the results string connectionString = "Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True;"; string storedProcedureName = "GetCustomers"; SqlParameter[] parameters = { new SqlParameter("@LastName", "Smith") }; using (SqlDataReader reader = SqlServerUtility.ExecuteSPReader(connectionString, storedProcedureName, parameters)) { while (reader.Read()) { int customerId = reader.GetInt32(reader.GetOrdinal("CustomerId")); string firstName = reader.GetString(reader.GetOrdinal("FirstName")); string lastName = reader.GetString(reader.GetOrdinal("LastName")); Console.WriteLine("{0}: {1}, {2}", customerId, lastName, firstName); } }
// Execute the "UpdateProduct" stored procedure and get the number of rows affected string connectionString = "Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True;"; string storedProcedureName = "UpdateProduct"; SqlParameter[] parameters = { new SqlParameter("@ProductId", 123), new SqlParameter("@Price", 19.99) }; int rowsAffected = SqlServerUtility.ExecuteSPNonQuery(connectionString, storedProcedureName, parameters); Console.WriteLine("{0} rows affected", rowsAffected);This example executes the "UpdateProduct" stored procedure with parameters for the product ID and the new price. It then gets the number of rows affected by the update and writes it to the console.