Exemple #1
0
        public void AddSellTransaction(String symbol, int qty, decimal sellPrice, Entities.PortfolioStock p, Entities.Portfolio up)
        {
            string sqlBuy = "INSERT INTO Transactions (PortfolioId, Type, Symbol, BuySellPrice, SharesBoughtSold, Date)"
                            + "VALUES (@PortfolioId, @Type, @Symbol, @Bit, @SharesBought, @Date)";

            SqlCommand cmd = new SqlCommand(sqlBuy, conn);

            cmd.Parameters.Add("@PortfolioId", SqlDbType.Int).Value  = p.PortfolioId;
            cmd.Parameters.Add("@Type", SqlDbType.NChar).Value       = "Sell";
            cmd.Parameters.Add("@Symbol", SqlDbType.NChar).Value     = symbol;
            cmd.Parameters.Add("@Bit", SqlDbType.Money).Value        = sellPrice;
            cmd.Parameters.Add("@SharesBought", SqlDbType.Int).Value = qty;
            cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value    = DateTime.Now;

            cmd.ExecuteNonQuery();

            string sqlUpdateCash = "Update Portfolio SET Cash=@Cash Where PortfolioId=@PortfolioId";

            SqlCommand cmdUpdate = new SqlCommand(sqlUpdateCash, conn);

            cmdUpdate.Parameters.Add("@PortfolioId", SqlDbType.Int).Value = p.PortfolioId;
            cmdUpdate.Parameters.Add("@Cash", SqlDbType.Money).Value      = up.Cash + (sellPrice * qty);
            cmdUpdate.ExecuteNonQuery();


            string sqlUpdatPortfolioStock = "Update PortfolioStock SET NumberOfSharesOwned=@NumberOfSharesOwned Where GameID=@GameID AND Symbol=@Symbol";

            SqlCommand cmdUpdatePortfolioStock = new SqlCommand(sqlUpdatPortfolioStock, conn);

            cmdUpdatePortfolioStock.Parameters.Add("@GameID", SqlDbType.Int).Value              = p.PortfolioId;
            cmdUpdatePortfolioStock.Parameters.Add("@Symbol", SqlDbType.NChar).Value            = symbol;
            cmdUpdatePortfolioStock.Parameters.Add("@NumberOfSharesOwned", SqlDbType.Int).Value = p.SharesOwned - qty;
            cmdUpdatePortfolioStock.ExecuteNonQuery();
        }
Exemple #2
0
        public static List <Entities.PortfolioStock> GetAll(Entities.Portfolio p)
        {
            List <Entities.PortfolioStock> result = new List <Entities.PortfolioStock>();


            string     SelecetUserPortfolio = "SELECT * FROM PortfolioStock WHERE GameID=@GameID";
            SqlCommand cmdGetPortfolioStock = new SqlCommand(SelecetUserPortfolio, Globals.Db.conn);

            cmdGetPortfolioStock.Parameters.Add("@GameID", SqlDbType.Int).Value = p.PortfolioID;

            using (SqlDataReader reader = cmdGetPortfolioStock.ExecuteReader())

            {
                while (reader.Read())
                {
                    string symbol = (string)reader["Symbol"];
                    Console.Write(symbol);
                    int     id = Convert.ToInt32(reader["GameID"]);
                    int     numberOfSharesOwned  = Convert.ToInt32(reader["NumberOfSharesOwned"]);
                    Decimal averagePurchasePrice = Convert.ToDecimal(reader["averagePurchasePrice"]);
                    Entities.PortfolioStock pn   = new Entities.PortfolioStock(symbol, id, numberOfSharesOwned, averagePurchasePrice);
                    result.Add(pn);
                }
            }
            return(result);
        }