private static void InsertIntoLatestScrape(Stocks stock) { string latestScrape = @"IF EXISTS(SELECT* FROM Stocks WHERE Symbol = @Symbol) UPDATE Stocks SET LastPrice = @LastPrice, ChangePercent = @ChangePercent, Volume = @Volume, AvgVol = @AvgVol, MarketCap = @MarketCap WHERE Symbol = @Symbol ELSE INSERT INTO Stocks VALUES(@Symbol, @LastPrice, @ChangePercent, @Volume, @AvgVol, @MarketCap);"; using (SqlConnection con = new SqlConnection(_connectionString)) { con.Open(); if (con.State == System.Data.ConnectionState.Open) { using (SqlCommand command = new SqlCommand(latestScrape, con)) { command.Parameters.Add(new SqlParameter("@Symbol", stock.Symbol)); command.Parameters.Add(new SqlParameter("@LastPrice", stock.LastPrice)); command.Parameters.Add(new SqlParameter("@ChangePercent", stock.ChangePercent)); command.Parameters.Add(new SqlParameter("@Volume", stock.Volume)); command.Parameters.Add(new SqlParameter("@AvgVol", stock.AvgVol)); command.Parameters.Add(new SqlParameter("@MarketCap", stock.MarketCap)); command.ExecuteNonQuery(); Console.WriteLine("{0} added to Stocks table...", stock.Symbol); } } else { Console.WriteLine("No connection..."); } con.Close(); } }
public static void InsertStockDataIntoDB(Stocks stock) { InsertIntoLatestScrape(stock); InsertIntoScrapeHistory(stock); }