예제 #1
0
        // gets all order IDs for the combo box
        public static List <int> GetOrderIDs()
        {
            List <int> orderIDs = new List <int>(); // empty list of IDs
            int        id;                          // for reading

            using (SqlConnection connection = NorthwindDB.GetConnection())
            {
                string query = "SELECT OrderID FROM Orders";
                // any exception not handled here is automatically thrown to the form
                // where the method was called
                using (SqlCommand cmd = new SqlCommand(query, connection))
                {
                    connection.Open();
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    // close connection as soon as done with reading
                    while (reader.Read())
                    {
                        id = (int)reader["OrderID"];
                        orderIDs.Add(id);
                    }
                } // command object recycled
            }     // connection object recycled
            return(orderIDs);
        }