我是编程新手。下面的c代码使用datagridview将单个表记录导出到json文件。数据库包含20多个表,每个表都有字段 last_updated
以及 added_on
. 如何更改此代码以将所有表中已过滤(使用日期时间选择器)的记录导出到json文件,而不使用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();
}
}
下面的c代码用于将datagridview视图内容转换为json文件
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();
}
1条答案
按热度按时间d8tt03nd1#
我使用以下方法