Yahoo Finance から見積もりをダウンロードしようとしていますが、過去数か月間は機能していましたが、1 週間後に処理できないエラーに直面しました。
最初にティッカー (GOOG など) を入力し、次に開始日と終了日を入力します。
しかし、すべてが適切に入力され、デバッグしようとすると、例外に直面します:
これが私のコードです:
private void button2_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
button2.Enabled = false;
symbol = cbSymbol.Text;
startDate = dtpStartDate.Value;
endDate = dtpEndDate.Value;
YahooStocks.Downloader dl = new Downloader();
DataTable dt = dl.UpdateSymbol(symbol, startDate, endDate);
dl.InsertOrUpdateIssue(dt, symbol);
dataGridView1.DataSource = dt;
SetControlProperty(label4, "Text", "GOT : " + symbol + " ");
try
{
this.DoChart(symbol, startDate, endDate);
refreshGraph();
tabControl1.Enabled = true;
}
catch (Exception ex)
{
label4.Text = ex.Message;
}
button2.Enabled = true;
Cursor.Current = Cursors.Default;
}
public DataTable UpdateSymbol(string symbol, DateTime? startDate, DateTime? endDate)
{
if (!endDate.HasValue) endDate = DateTime.Now;
if (!startDate.HasValue) startDate = DateTime.Now.AddYears(-5);
if (symbol == null || symbol.Length < 1)
throw new ArgumentException("Symbol invalid: " + symbol);
// NOTE: Yahoo's scheme uses a month number 1 less than actual e.g. Jan. ="0"
int strtMo = startDate.Value.Month - 1;
string startMonth = strtMo.ToString();
string startDay = startDate.Value.Day.ToString();
string startYear = startDate.Value.Year.ToString();
int endMo = endDate.Value.Month - 1;
string endMonth = endMo.ToString();
string endDay = endDate.Value.Day.ToString();
string endYear = endDate.Value.Year.ToString();
urlTemplate = urlTemplate.Replace("[symbol]", symbol);
urlTemplate = urlTemplate.Replace("[startMonth]", startMonth);
urlTemplate = urlTemplate.Replace("[startDay]", startDay);
urlTemplate = urlTemplate.Replace("[startYear]", startYear);
urlTemplate = urlTemplate.Replace("[endMonth]", endMonth);
urlTemplate = urlTemplate.Replace("[endDay]", endDay);
urlTemplate = urlTemplate.Replace("[endYear]", endYear);
string history = String.Empty;
WebClient wc = new WebClient();
try
{
history = wc.DownloadString(urlTemplate);
}
catch (WebException wex)
{
// throw wex;
}
finally
{
wc.Dispose();
}
DataTable dt = new DataTable();
// trim off unused characters from end of line
history = history.Replace("\r", "");
// split to array on end of line
string[] rows = history.Split('\n');
// split to colums
string[] colNames = rows[0].Split(',');
// add the columns to the DataTable
foreach (string colName in colNames)
dt.Columns.Add(colName);
DataRow row = null;
string[] rowValues;
object[] rowItems;
// split the rows
for (int i = rows.Length - 1; i > 0; i--)
{
rowValues = rows[i].Split(',');
row = dt.NewRow();
rowItems = ConvertStringArrayToObjectArray(rowValues);
if (rowItems[0] != null && (string)rowItems[0] != "")
{
row.ItemArray = rowItems;
dt.Rows.Add(row);
}
}
return dt;
}
public static int ExecuteNonQuery(string connectionString, string commandText, object[] paramList)
{
SQLiteConnection cn = new SQLiteConnection(connectionString);
SQLiteCommand cmd = cn.CreateCommand();
cmd.CommandText = commandText;
cmd = DeriveParameters(cmd, paramList);
if (cn.State == ConnectionState.Closed)
cn.Open();
int result = cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();
return result;
}