I am creating a ERP tool for a supermarket. The owner has two supermarkets in two different places. So, to manage the supermarket the local database (mySQL) should be synchronized to the web server.
Currently I am using the following C# code to export all records of a table(sales_products
) from my database by filtering the records using columns added_on
and last_updated
. My database contains more than 20 tables and more records.
private void button1_Click(object sender, EventArgs e)
{
string json = string.Empty;
List<object> objects = new List<object>();
MySqlConnection _dbConnection = new MySqlConnection("Server = localhost; Database = app_erp_suneka; Uid = root; Pwd = ;");
{
_dbConnection.Open();// .Open();
using (MySqlCommand command = _dbConnection.CreateCommand())
{
command.CommandText = "SELECT * FROM sales_products";
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
IDictionary<string, object> record = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
record.Add(reader.GetName(i), reader[i]);
}
objects.Add(record);
}
}
}
}
json = JsonConvert.SerializeObject(objects);
using (StreamWriter sw = new StreamWriter(File.Create(@"C:\Users\SAKTHY-PC\Desktop\path.json")))// "C:\\path\\file.json")))
{
sw.Write(json);
}
}
My Question is:
How can I export all records to json file from all tables
using C# ?