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

标签 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

最佳答案

您可以将 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 变量。它们的值将是循环结束时最后一条记录的值。也许您想将返回值存储在数组或列表中。

关于c# - MySQL存储过程只返回最后一行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701668/

相关文章:

c# - unity填充量lerp返回错误值

c# - 如何在 .net Core 应用程序中使用 IConfiguration 绑定(bind)多级配置对象?

c# - WPF(C#) 如何使用post请求传输文件?

c# - 如何舍入一个数字

mysql - 在mysql查询中连接同一个表两次

mysql - 从 Docker 卷恢复 mysql 数据

c# - SortedDictionary 将一个 SortedDictionary 添加到另一个

mysql - 返回符合 phpmyadmin 数据库中一组特定条件的用户

python pymysql设置autocommit false失败

javascript - for循环中的node.js mysql查询