MySQL stored procedure returns only the last row of data

I am calling the MySQL stored procedure, which should return about 6,000 rows. But it only returns the last row. Looking at this, I can’t understand why it will return only the last line of information.

C # code

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 stored procedure

 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 
+5
source share
2 answers

You can simplify sql as:

 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 

This will surely return all characters from the company table along with their last histDate value.

EDIT

In a C # while loop, you are rewriting the variables stockSymbol and stockLastUpdate. Their value will be the last record value at the end of the loop. Perhaps you want to store the return values ​​in an array or list.

0
source

Try adding myCommand.CommandType = CommandType.StoredProcedure;

0
source

Source: https://habr.com/ru/post/1240258/


All Articles