I am new to programming. The following c# code uses the datagridview to export a single table records to json file. The database contains more than 20 table and every tables has fields last_updated
and added_on
. How can I change this code to export the filtered(using date time picker) records from all tables to json file without using datagridview.
private void btnFilldataGridView_Click(object sender, EventArgs e)
{
try
{
_dbConnection.Open();
const string selectQuery =
"SELECT * FROM purchases WHERE (last_updated <= @dtp_last_updated) AND (added_on <= @dtp_last_updated)";
using (var cmdLocal = new MySqlCommand(selectQuery, _dbConnection))
{
cmdLocal.Parameters.Add("@dtp_last_updated", MySqlDbType.DateTime).Value =
DateTime.Parse(dtpLastServerUpdated.Text);
cmdLocal.Connection = _dbConnection;
cmdLocal.CommandText = selectQuery;
_dbDataAdapter = new MySqlDataAdapter();
_dbDataAdapter.SelectCommand = cmdLocal;
_dbDataTable = new DataTable();
_dbDataAdapter.Fill(_dbDataTable);
dataGridView1.DataSource = _dbDataTable;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
_dbDataAdapter.Dispose();
_dbConnection.Close();
}
}
The following c# coding is used to convert the datagridview view content to json file
private void btnExportToJson_Click(object sender, EventArgs e)
{
var jasonData = (DataTableToJson(_dbDataTable));
//MessageBox.Show(afd);
System.IO.File.WriteAllText(@"C:\Users\SAKTHY-PC\Desktop\path.json", jasonData);
Application.Exit();
}