新增資料存入Class
public static DataView GetStocksCode()
{
MySqlDataReader reader = null;
var serverName = "10.10.10.101";
var uidName = "root";
var pwdName = "1234";
var databaseName = "BDS";
string connStr = String.Format("server={0};uid={1};pwd={2};database={3}",
serverName, uidName, pwdName, databaseName);
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(connStr);
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM STOCKSCODE", conn);
reader = cmd.ExecuteReader();
DataView myDataView = PopulateDataView(reader, "myTable");
return myDataView;
//while (reader.Read())
//{
// listBox2.Items.Add(reader.GetString(0));
//}
}
catch (MySqlException ex)
{
return null;
// MessageBox.Show("Failed to populate database list: " + ex.Message);
}
finally
{
if (reader != null) reader.Close();
}
}
1.新增資料存入Class, 擷取部分程式
private void button6_Click(object sender, EventArgs e)
{
string stock_id = "";
label1.Text = "0";
// 讀取所有股票列表
DataView myDataView = DAL.GetStocksData.GetStocksCode();
// 如果沒有 C:/Stocks/ 目錄, 則建立
if (!Directory.Exists("C:/Stocks/")) Directory.CreateDirectory("C:/Stocks/");
// 所有股票列表
foreach (DataRowView myDRV in myDataView)
{
listBox2.Items.Add(myDRV["code"].ToString());
stock_id = myDRV["code"].ToString();
FileStream fs = new FileStream("C:/Stocks/" + stock_id + ".csv", FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding(950));
sw.WriteLine("股市代號,日期,收盤價");
var SQLCommand = string.Empty;
// 自1999年到2014年
for (int y = 1999; y <= 2014; y++)
{
// 自1到12月
for (int m = 1; m <= 12; m++)
{
// 讀取 getMonthData
List<string> month_data = getMonthData(stock_id, y, m);
for (int i = 0; i < month_data.Count; i++)
{
// 產生 SQLCommand
SQLCommand = "INSERT INTO stockshistory (StocksCode,Date,Price) VALUES('";
SQLCommand += month_data[i].Replace(",", "','");
SQLCommand += "')";
int intStatus = GetStocksData.RunSQLCommand(SQLCommand);
label1.Text = (Convert.ToDouble(label1.Text) + 1).ToString();
// 寫入資料
sw.WriteLine(month_data[i]);
}
// 秀在Console
Console.WriteLine(y + "/" + m + (month_data.Count == 0 ? " no data." : " download."));
}
}
sw.Close();
fs.Close();
}
}
2.改寫Buttom6, 增加
(1) 讀出股票代碼
(2)產生SQL command