MySQL存储过程只返回最后一行数据

时间:2016-01-10 03:18:08

标签: c# mysql

我正在调用一个应该返回大约6000行的MySQL存储过程。但它只返回最后一排。看了之后,我无法理解为什么它只返回最后一行信息。

C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Net;
using FileHelpers;
using Stockton;
using System.IO;

namespace MassHistoricalDownload
{
    class Program
    {

        MySqlConnection conn = new MySqlConnection();
        Reference r = new Reference();

        static void Main(string[] args)
        {
            Program p = new Program();
            p.DownloadHistoricData();
        }

        public void DownloadHistoricData()
        {
            conn.ConnectionString = r.getMysqlConnection();
            string historicalDataPath = r.getHistFileLocation();
            string historicalDataExtension = ".csv";

            //Get the list of ticker symbols as well as the last date for which historical data was downloaded for that stock
            conn.Open();
            MySqlCommand myCommand = new MySqlCommand("call stockton.pullHistSymbolAndDate()", conn);
            MySqlDataReader rdr = myCommand.ExecuteReader();

            //Download the files into the HistoricalData file
            while (rdr.Read())
            {
                string stockSymbol = rdr["symbol"].ToString();
                string stockLastDate = rdr["histDate"].ToString();

                //Check if the stockLastDate is empty and put int the oldest date if it is
                if (stockLastDate == null || stockLastDate == string.Empty)
                    stockLastDate = r.getOldestTradeDate().ToString();

                using (WebClient client = new WebClient())
                {
                    using (StreamWriter sw = File.AppendText(Path.Combine(historicalDataPath, stockSymbol + historicalDataExtension)))
                    {

                        sw.Write(client.DownloadString(string.Format(r.getYahooDownloadPart(), stockSymbol, stockLastDate)));
                    }
                }
            }
            conn.Close();
        }
    }

    [IgnoreFirst(1)]
    [DelimitedRecord(",")]
    public class HistoricalStock
    {
        public string infoDate { get; set; }
        public double stockOpen { get; set; }
        public double stockHigh { get; set; }
        public double stockLow { get; set; }
        public double stockClose { get; set; }
        public int stockVolume { get; set; }
        public double adjClose { get; set; }
    }
}

MySQL存储过程

CREATE DEFINER=`meggleston`@`%` PROCEDURE `pullHistSymbolAndDate`()
BEGIN
select
    c.symbol,
    hd.histDate
from
    company c
    left join historical_data hd on
        hd.symbol = c.symbol
        and 
        hd.histDate = (select max(histDate) from historical_data where symbol = c.symbol);
END

2 个答案:

答案 0 :(得分:0)

您可以将sql简化为:

select
    c.symbol,
    max(hd.histDate) as histDate
from
    company c
    left join historical_data hd on hd.symbol = c.symbol
    group by c.symbol

这肯定会返回公司表中的所有符号以及它们的最后一个histDate。

修改

在c#while循环中,您将覆盖stockSymbol和stockLastUpdate变量。它们的值将是循环结束时的最后一个记录值。您可能希望将返回的值存储在数组或列表中。

答案 1 :(得分:0)

尝试添加myCommand.CommandType = CommandType.StoredProcedure;