Migrating data from Access 97 to Postgres database
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

64 lines
2.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.SshNet;
  6. using Npgsql;
  7. namespace migrate_data
  8. {
  9. public class PGHelper
  10. {
  11. public void MakeSSHTunnel(string tblName = null, List<string> sql = null )
  12. {
  13. using (SshClient client = new SshClient("192.168.0.108", "mcarman", "@ng31F@rm0823262"))
  14. {
  15. int index = 0;
  16. client.Connect();
  17. var tunnel = new ForwardedPortLocal("127.0.0.1", "127.0.0.1", 5432);
  18. client.AddForwardedPort(tunnel);
  19. if (client.IsConnected)
  20. {
  21. tunnel.Start();
  22. string connString = string.Format("Server={0};Database=accessdb_v2;Port={1};User Id=mcarman;Password=@ng31F@rm0823262;", tunnel.BoundHost, tunnel.BoundPort );
  23. try
  24. {
  25. using (var conn = new NpgsqlConnection(connString))
  26. {
  27. System.Data.DataTable tbl = new System.Data.DataTable("user");
  28. conn.Open();
  29. NpgsqlCommand cmd = conn.CreateCommand();// new NpgsqlCommand();
  30. cmd.CommandType = System.Data.CommandType.Text;
  31. foreach (string s in sql)
  32. {
  33. cmd.CommandText = s;
  34. int cmdInt = cmd.ExecuteNonQuery();
  35. Console.WriteLine(string.Format("Row {0} Updatede - Command Result: {1}", index, cmdInt));
  36. index += 1;
  37. }
  38. //("select * from \"User\";", conn);
  39. // cmd.CommandType = System.Data.CommandType.Text;
  40. //NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
  41. //adapter.Fill(tbl);
  42. Console.Write("Yes");
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. Console.Write(ex.Message);
  48. }
  49. }
  50. else Console.Write("Broken");
  51. }
  52. }
  53. public void CloseSSHTunnel(SshClient client)
  54. {
  55. client.Disconnect();
  56. }
  57. }
  58. }