c#

  • Thread starter Thread starter tharminianandasivanesan
  • Start date Start date
T

tharminianandasivanesan

Guest
How to get .csv file data by start date, start time and end date, end time

i am getting the .csv file to the Windows Form Application through a link label the code is below

public static Task<DataTable> CopyCsvToDataTable(string filePath)
{
return Task.Run<DataTable>(() => {

if (!File.Exists(filePath))
throw new ArgumentException("File does not exist in the selected path.");
StreamReader sr = new StreamReader(filePath);
string[] headers = sr.ReadLine().Split(',');
var dt = new DataTable();
foreach (string header in headers)
{
dt.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = Regex.Split(sr.ReadLine(), ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
if (rows.Length <= i)
continue;
dr = rows;
}
dt.Rows.Add(dr);
}
return dt;
});

}
public string filepath { get; set; }
private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{

OpenFileDialog op = new OpenFileDialog();
op.Filter = "CSV File (*.csv)|*.csv";
op.InitialDirectory = @"D:\";
op.Title = "Please select the path of the CSV file";
linkLabel1.Enabled = false;
//btnSearch.Enabled = false;
//btnPrint.Enabled = false;
Cursor.Current = Cursors.WaitCursor;
if (op.ShowDialog() == DialogResult.OK)
{
filepath = op.FileName;
var m = CopyCsvToDataTable(filepath);
DataTable dt = m.Result;
dataGridView1.DataSource = dt;
}


this is one of my where i select data by start date, start time and end date, end time,

thisi used to get data from MSSQL, how to do it for .csv files

DateTime starttime = dtpstartdate.Value.Add(dtpstarttime.Value.TimeOfDay);
DateTime endtime = dtpenddate.Value.Add(dtpendtime.Value.TimeOfDay);
{
String cmdString = "select Date, Time, " + cmbMachines.Text + "" +
" from " + cmbTables.Text + "" +
$" where (CONVERT(DATETIME, DATE) + CONVERT(DATETIME, TIME)) between '{ starttime }' and '{ endtime }'";
SqlCommand cmd = new SqlCommand(cmdString, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView.DataSource = dt;

con.Open();
cmd.ExecuteNonQuery();
con.Close();

Continue reading...
 
Back
Top