Migrating data from Access 97 to Postgres database
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

92 lignes
3.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. namespace acc_pgsql
  7. {
  8. public static class PGSqlGenerator
  9. {
  10. static string _tblDescription = @"select column_name, data_type, character_maximum_length, column_default, is_nullable from INFORMATION_SCHEMA.COLUMNS where table_name = '{0}' order by ordinal_position;";
  11. public struct PGColumn
  12. {
  13. public string name { get; set; }
  14. public bool singlequotesneeded { get; set; }//This is for the value when selecting
  15. }
  16. public static void GenerateSeedData(string tblName)
  17. {
  18. StringBuilder sb = new StringBuilder();
  19. string sql = string.Format(_tblDescription, tblName);
  20. List<string> list = new List<string>();
  21. list.Add(sql);
  22. list.Add(string.Format("select * from \"{0}\"", tblName));
  23. System.Data.DataSet ds = PGSqlHelper.RunPGSql(list);
  24. string insert = "Insert Into \"" + tblName + "\"(";
  25. List<PGColumn> fields = new List<PGColumn>();
  26. foreach (DataRow row in ds.Tables[0].Rows)
  27. {
  28. PGColumn clmn = new PGColumn();
  29. clmn.name = row.ItemArray[0].ToString();
  30. switch (row.ItemArray[1].ToString())
  31. {
  32. case "bigint":
  33. clmn.singlequotesneeded = false;
  34. break;
  35. case "timestamp with time zone":
  36. clmn.singlequotesneeded = true;
  37. break;
  38. case "text":
  39. clmn.singlequotesneeded = true;
  40. break;
  41. case "boolean":
  42. clmn.singlequotesneeded = false;
  43. break;
  44. default:
  45. clmn.singlequotesneeded = true;
  46. break;
  47. }
  48. fields.Add(clmn);
  49. }
  50. string topline = string.Format("Insert Into \"{0}\"(", tblName);
  51. string bottomline = "values(";
  52. for(int i = 0; i < fields.Count; i++)
  53. {
  54. if (i == 0)
  55. {
  56. topline = topline + "\"" + fields[i].name + "\"";
  57. if (fields[i].singlequotesneeded)
  58. bottomline = bottomline + string.Format("'{0}'", "|"+i+"|");
  59. else bottomline = bottomline + string.Format("{0}", "|" + i + "|");
  60. }
  61. else
  62. {
  63. topline = topline + ", \"" + fields[i].name + "\"";
  64. if (fields[i].singlequotesneeded)
  65. bottomline = bottomline + string.Format(", '{0}'", "|" + i + "|");
  66. else bottomline = bottomline + string.Format(", {0}", "|" + i + "|");
  67. }
  68. }
  69. topline = topline + ")";
  70. bottomline = bottomline + ");";
  71. string statment = string.Format("{0} {1}", topline, bottomline);
  72. List<string> insertstatements = new List<string>();
  73. foreach (DataRow row2 in ds.Tables[1].Rows)
  74. {
  75. for(int c = 0; c < ds.Tables[1].Columns.Count; c++)
  76. {
  77. string rep = string.Format("|{0}|", c);
  78. statment = statment.Replace(rep, row2.ItemArray[c].ToString());
  79. }
  80. insertstatements.Add(statment);
  81. sb.AppendLine(statment);
  82. }
  83. Console.WriteLine(sb.ToString());
  84. }
  85. }
  86. }