コード例 #1
0
        // "Create" is a more general name for adding new data, return type is changed to "void"
        // AFTER
        // 10 microseconds
        public void CreateRegistryReferral(int grId, int referrerId, int referrerLinkId)
        {
            try
            {
                using (SqlConnection connection = new CrateSqlConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "dbo.RegistryData_InsertReferral";

                        command.Parameters.AddWithValue("@grId", grId);  // use convenience method to avoid verbose settings
                        command.Parameters.AddWithValue("@referrerID", referrerId);
                        command.Parameters.AddWithValue("@referrerLinkID", referrerLinkId);
                        command.Parameters.AddWithValue("@last_mod_user", "CreateRegistryReferral");  // need a better key

                        connection.Open();

                        if (command.ExecuteNonQuery() == 0)
                        {
                            throw new InvalidOperationException("Failed to update");  // consider using a custom exception to be more specific
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Failed to update", ex);  // wrap SqlException and communicate as a general action failure message; make stored procedure return specific error message(code) for proper message
            }
        }
コード例 #2
0
        // this is still not unit test friendly
        public string SelectTransactionSeed_Refactored()
        {
            int seed = 0;

            try
            {
                using (SqlConnection connection = new CrateSqlConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "dbo.TransactionIDData_SelectTransActionID";
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                seed = (int)reader["Seed"];

                                WriteTraceWarn("Returning: " + seed.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // wrap SqlException and communicate as a general action failure message; make stored procedure return specific error message(code) for proper message
                throw new InvalidOperationException("Failed to get data", ex);
            }

            return ConvertLast4DigitsToString(seed);
        }