-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cs
143 lines (116 loc) · 6.37 KB
/
Database.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace CSharp_Scraper
{
class Database
{
private const string _connection = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=StockTable;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
public static void InsertScrapeToDatabase(Stock stock)
{
DataToTable(stock);
}
public static void LatestScrapeToDatabase(Stock stock)
{
LastScrapeToDatabase(stock);
}
public static void Clear_Reset()
{
DeleteTableData();
ResetAutoIncrementer();
}
public static void DeleteTableData()
{
string reseed = "DBCC CHECKIDENT ('StockHistory', RESEED, 0);";
using (SqlConnection con = new SqlConnection(_connection))
{
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
using (SqlCommand cmd = new SqlCommand(reseed, con))
{
cmd.ExecuteNonQuery();
}
}
con.Close();
}
}
public static void ResetAutoIncrementer()
{
string reseed = "DBCC CHECKIDENT ('StockHistory', RESEED, 0);";
}
private static void LastScrapeToDatabase(Stock stock)
{
string lastScrape = @"IF EXISTS(SELECT* FROM dbo.LastTable WHERE Stock_Symbol = @stock_symbol)
UPDATE dbo.LastTable
SET Time_Scraped = @time_scraped, Last_Price = @last_price, Change = @Change, Change_Percent = @change_percent,
Volume = @volume, Shares = @shares, Average_Volume = @average_volume, Market_Cap = @market_cap
WHERE Stock_Symbol = @stock_symbol
ELSE
INSERT INTO dbo.LastTable VALUES (@time_scraped, @stock_symbol, @last_price, @change, @change_percent, @volume, @shares, @average_volume, @market_cap);";
using (SqlConnection db = new SqlConnection(_connection))
{
db.Open();
Console.WriteLine("Database has been opened.");
if (db.State == System.Data.ConnectionState.Open)
{
using (SqlCommand command = new SqlCommand(lastScrape, db))
{
command.Parameters.AddWithValue("@time_scraped", stock.TimeScraped);
command.Parameters.AddWithValue("@stock_symbol", stock.StockSymbol);
command.Parameters.AddWithValue("@last_price", stock.LastPrice);
command.Parameters.AddWithValue("@change", stock.Change);
command.Parameters.AddWithValue("@change_percent", stock.ChangePercent);
command.Parameters.AddWithValue("@volume", stock.Volume);
command.Parameters.AddWithValue("@shares", stock.Shares);
command.Parameters.AddWithValue("@average_volume", stock.AvgVol);
command.Parameters.AddWithValue("@market_cap", stock.MarketCap);
command.ExecuteNonQuery();
}
}
else
{
Console.WriteLine("No database found. Please check database connection.");
}
db.Close();
}
}
/*string[] stockFields = { "@time_scraped", "@stock_symbol", "@last_price", "@change", "@change_percent", "@volume", "@shares", "@average_volume", "@market_cap" };
string[] stockData = { System.Convert.ToString(DateTime.Now), "TEST", "$", "1", "1%", "2", "2", "2", "2" };*/
private static void DataToTable(Stock stock)
{
using (SqlConnection db = new SqlConnection(_connection))
{
string insertToTable = "INSERT INTO dbo.StockTable (Time_Scraped, Stock_Symbol, Last_Price, Change, Change_Percent, Volume, Shares, Average_Volume, Market_Cap) VALUES (@time_scraped, @stock_symbol, @last_price, @change, @change_percent, @volume, @shares, @average_volume, @market_cap);";
{
db.Open();
Console.WriteLine("Database has been opened");
if (db.State == System.Data.ConnectionState.Open)
{
using (SqlCommand dataToTable = new SqlCommand(insertToTable, db))
{
dataToTable.Parameters.AddWithValue("@time_scraped", stock.TimeScraped);
dataToTable.Parameters.AddWithValue("@stock_symbol", stock.StockSymbol);
dataToTable.Parameters.AddWithValue("@last_price", stock.LastPrice);
dataToTable.Parameters.AddWithValue("@change", stock.Change);
dataToTable.Parameters.AddWithValue("@change_percent", stock.ChangePercent);
dataToTable.Parameters.AddWithValue("@volume", stock.Volume);
dataToTable.Parameters.AddWithValue("@shares", stock.Shares);
dataToTable.Parameters.AddWithValue("@average_volume", stock.AvgVol);
dataToTable.Parameters.AddWithValue("@market_cap", stock.MarketCap);
dataToTable.ExecuteNonQuery();
}
}
else
{
Console.WriteLine("No database found. Please check database connection.");
}
db.Close();
}
}
}
}
}