示例#1
0
        /// <summary>
        /// This method accepts a string of a stock symbol and adds it to the
        /// database's stock table. It is typically only called when the program
        /// first collects stock data or different markets (with different symbols)
        /// are queried.
        /// </summary>
        /// <param name="symbol">This is a string of the symbol to be added to the database.</param>
        /// <returns>Returns and integer containing the database ID of the stock that was added,
        /// or a -1 if the stock failed to add. If the stock already exists, the id of the stock
        /// in the database is returned.</returns>
        public int addStockSymbol(string symbol)
        {
            int success = -1;

            using (var db = new stock_advisorDataContext(Program.ConnectionString))
            {
                //create user_info object w/params
                var newEntry = new STOCK()
                {
                    Symbol = symbol
                };

                STOCK search = db.STOCKs.Where(r => r.Symbol == symbol).SingleOrDefault();
                if (search == null) //record doesn't exist yet...
                {
                    db.STOCKs.InsertOnSubmit(newEntry);
                    try
                    {
                        db.SubmitChanges(); //execute insert
                        //verify that was successfully inserted
                        var added = db.STOCKs.SingleOrDefault(a => a.Symbol == symbol);
                        if (added != null)
                        {
                            success = added.id;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception caught during stock creation/insert:\n");
                        Console.WriteLine(e.Message);
                    }
                }
                else    //record exists!
                {
                    success = search.id;
                }
            }
            return(success);
        }
 partial void UpdateSTOCK(STOCK instance);
 partial void DeleteSTOCK(STOCK instance);
 partial void InsertSTOCK(STOCK instance);