如何使用c将数据库中的所有表记录导出为json#

lf5gs5x2  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(294)

我正在为一家超市创建一个erp工具。店主在两个不同的地方有两家超市。因此,为了管理超市,本地数据库(mysql)应该与web服务器同步。目前,我正在使用以下c代码导出表的所有记录( sales_products )从我的数据库中,通过使用列筛选记录 added_on 以及 last_updated . 我的数据库包含20多个表和更多记录。

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. string json = string.Empty;
  4. List<object> objects = new List<object>();
  5. MySqlConnection _dbConnection = new MySqlConnection("Server = localhost; Database = app_erp_suneka; Uid = root; Pwd = ;");
  6. {
  7. _dbConnection.Open();// .Open();
  8. using (MySqlCommand command = _dbConnection.CreateCommand())
  9. {
  10. command.CommandText = "SELECT * FROM sales_products";
  11. using (MySqlDataReader reader = command.ExecuteReader())
  12. {
  13. while (reader.Read())
  14. {
  15. IDictionary<string, object> record = new Dictionary<string, object>();
  16. for (int i = 0; i < reader.FieldCount; i++)
  17. {
  18. record.Add(reader.GetName(i), reader[i]);
  19. }
  20. objects.Add(record);
  21. }
  22. }
  23. }
  24. }
  25. json = JsonConvert.SerializeObject(objects);
  26. using (StreamWriter sw = new StreamWriter(File.Create(@"C:\Users\SAKTHY-PC\Desktop\path.json")))// "C:\\path\\file.json")))
  27. {
  28. sw.Write(json);
  29. }
  30. }

我的问题是:
如何将所有记录从导出到json文件 all tables 使用c#?

7qhs6swi

7qhs6swi1#

@布拉德利·格兰杰,很高兴听到。但我不能保证,我的本地数据库一直有最少的记录使用jsonserializer。在功能时间(如新年或圣诞节…),将有更多的交易,所以数据库将巨大的。

sdnqo3pr

sdnqo3pr2#

json只有有限的数据类型(字符串、浮点数、布尔值、, null ); 将mysql数据导出为json可能会导致精度下降(因为 DATETIME , TIMESTAMP , GUID , BLOB 等,将必须转换为字符串)。
但是,如果您仍然希望将数据库导出为json,首先需要找到数据库中的所有表(通过查询 information_schema.tables 表),然后遍历每个表,选择所有行并将它们转储到json。因为这可能是大量的数据,为了避免内存不足,您需要将结果流式传输到输出文件(而不是在内存中创建大量对象,然后将它们转换为json)。这需要使用低级别的json编写api,因此您需要确保 WriteStartObject 以及 WriteEndObject 调用正确配对以创建有效的json。
以下程序段演示了此技术:

  1. using (var connection = new MySqlConnection("Server = localhost; Database = app_erp_suneka; Uid = root; Pwd = ;"))
  2. {
  3. connection.Open();
  4. // get the names of all tables in the chosen database
  5. var tableNames = new List<string>();
  6. using (var command = new MySqlCommand(@"SELECT table_name FROM information_schema.tables where table_schema = @database", connection))
  7. {
  8. command.Parameters.AddWithValue("@database", "app_erp_suneka");
  9. using (var reader = command.ExecuteReader())
  10. {
  11. while (reader.Read())
  12. tableNames.Add(reader.GetString(0));
  13. }
  14. }
  15. // open a JSON file for output; use the streaming JsonTextWriter interface to avoid high memory usage
  16. using (var streamWriter = new StreamWriter(@"C:\Temp\app_erp_suneka.json"))
  17. using (var jsonWriter = new JsonTextWriter(streamWriter) { Formatting = Newtonsoft.Json.Formatting.Indented, Indentation = 2, IndentChar = ' ' })
  18. {
  19. // one array to hold all tables
  20. jsonWriter.WriteStartArray();
  21. foreach (var tableName in tableNames)
  22. {
  23. // an object for each table
  24. jsonWriter.WriteStartObject();
  25. jsonWriter.WritePropertyName("tableName");
  26. jsonWriter.WriteValue(tableName);
  27. jsonWriter.WritePropertyName("rows");
  28. // an array for all the rows in the table
  29. jsonWriter.WriteStartArray();
  30. // select all the data from each table
  31. using (var command = new MySqlCommand($@"SELECT * FROM `{tableName}`", connection))
  32. using (var reader = command.ExecuteReader())
  33. {
  34. while (reader.Read())
  35. {
  36. // write each row as a JSON object
  37. jsonWriter.WriteStartObject();
  38. for (int i = 0; i < reader.FieldCount; i++)
  39. {
  40. jsonWriter.WritePropertyName(reader.GetName(i));
  41. jsonWriter.WriteValue(reader.GetValue(i));
  42. }
  43. jsonWriter.WriteEndObject();
  44. }
  45. }
  46. jsonWriter.WriteEndArray();
  47. jsonWriter.WriteEndObject();
  48. }
  49. jsonWriter.WriteEndArray();
  50. }
  51. }
展开查看全部

相关问题