0

我正在尝试从 Yahoo Finance 下载报价,但它在过去几个月内确实有效,并且一周以来我遇到了一个我无法处理的错误:

我首先输入代码(例如 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;
}
4

0 回答 0