Beispiel #1
0
        /// <summary>
        /// Update the shipping date for a single order
        /// </summary>
        /// <param name="oldOrder">The old version of the order, for the order ID</param>
        /// <param name="newDate">The new version of the date</param>
        public static void UpdateOrder(Order oldOrder, DateTime?newDate)
        {
            using (SqlConnection conn = NorthwindDB.GetConnection())                                  // get the connection to the DB
            {
                string query = "UPDATE Orders SET ShippedDate=@ShippedDate WHERE OrderID = @OrderID"; // we'd only ever change shipped date in this application

                using (SqlCommand cmd = new SqlCommand(query, conn))                                  // initialize and use the query object
                {
                    cmd.Parameters.AddWithValue("@OrderID", oldOrder.OrderID);
                    // if the date is null, use DBnull. Otherwise use the date
                    if (newDate == null)
                    {
                        cmd.Parameters.AddWithValue("@ShippedDate", DBNull.Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@ShippedDate", newDate);
                    }

                    conn.Open();
                    cmd.ExecuteNonQuery(); // run the command
                }
            }
        }