我是编程新手。以下 c# 代码使用 datagridview 将单表记录导出到 json 文件。该数据库包含 20 多个表,每个表都有字段last_updated和added_on. 如何更改此代码以在不使用 datagridview 的情况下将所有表中过滤后的(使用日期时间选择器)记录导出到 json 文件。
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();
}
相关分类