I have 1000 csv files, each csv file contains 5500 rows of data and 8 columns. My code below took 5 minutes to import 1 csv file into MySQL database. I know there are many questions like this out there, I've been looking into them. But, is there any suggestions you have to improve this import process, according to my code?
private void btn_fetchSharePrices_Click(object sender, EventArgs e)
{
string[] fileCSV = Directory.GetFiles(sourceDirCSV);
foreach (string csv in fileCSV)
{
try
{
string[] lines = File.ReadAllLines(csv);
foreach (var line in lines)
{
var data = line.Split(new[] { ',' }, 8);
DateTime prices_date = DateTime.Parse(data[0].Trim());
DateTime prices_time = DateTime.Parse(data[1].Trim());
string open = data[2].Trim();
string high = data[3].Trim();
string low = data[4].Trim();
string close = data[5].Trim();
int volume = int.Parse(data[6].Trim());
int tickers_ticker_id = int.Parse(data[7].Trim());
StoreRecord_FetchSharePrices(prices_date, prices_time, open, high, low, close, volume, tickers_ticker_id);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void StoreRecord_FetchSharePrices(DateTime prices_date, DateTime prices_time, string open, string high, string low, string close, int volume, int tickers_ticker_id)
{
using (var connection = new MySqlConnection(strProvider))
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = @"INSERT IGNORE INTO prices (Prices_Date, Prices_Time, Prices_Open, Prices_High, Prices_Low, Prices_Close, Prices_Volume, Tickers_Ticker_ID) VALUES (@Prices_Date, @Prices_Time, @Prices_Open, @Prices_High, @Prices_Low, @Prices_Close, @Prices_Volume, @Tickers_Ticker_ID)";
command.Parameters.AddWithValue("@Prices_Date", prices_date);
command.Parameters.AddWithValue("@Prices_Time", prices_time);
command.Parameters.AddWithValue("@Prices_Open", open);
command.Parameters.AddWithValue("@Prices_High", high);
command.Parameters.AddWithValue("@Prices_Low", low);
command.Parameters.AddWithValue("@Prices_Close", close);
command.Parameters.AddWithValue("@Prices_Volume", volume);
command.Parameters.AddWithValue("@Tickers_Ticker_ID", tickers_ticker_id);
command.ExecuteNonQuery();
}
}