我正在调用一个应该返回大约 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/