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.
 
 
 

67 lignes
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 acc_pgsql
  8. {
  9. public static class PGSqlHelper
  10. {
  11. static string _ipaddress = "192.168.0.108";
  12. static string _username = "mcarman";
  13. static string _pwd = "@ng31F@rm0823262";
  14. static string _localip = "127.0.0.1";
  15. static uint _port = (uint)5432;
  16. static string _connString = "Server={0};Database=accessdb_v2;Port={1};User Id={2};Password={3};";
  17. //allway wrap call in try catch
  18. public static System.Data.DataSet RunPGSql( List<string> sql)
  19. {
  20. uint port = (uint)_port;
  21. using (SshClient client = new SshClient(_ipaddress, _username, _pwd))
  22. {
  23. System.Data.DataSet ds = new System.Data.DataSet();
  24. client.Connect();
  25. ForwardedPortLocal tunnel = new ForwardedPortLocal(_localip, _localip, _port);
  26. client.AddForwardedPort(tunnel);
  27. if (client.IsConnected)
  28. {
  29. tunnel.Start();
  30. string connStr = string.Format(_connString, tunnel.BoundHost, tunnel.BoundPort, _username, _pwd);
  31. try
  32. {
  33. using (NpgsqlConnection conn = new NpgsqlConnection(connStr))
  34. {
  35. conn.Open();
  36. NpgsqlCommand cmd = conn.CreateCommand();
  37. cmd.CommandType = System.Data.CommandType.Text;
  38. foreach (string s in sql)
  39. {
  40. System.Data.DataTable tbl = new System.Data.DataTable();
  41. cmd.CommandText = s;
  42. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
  43. adapter.Fill(tbl);
  44. ds.Tables.Add(tbl);
  45. }
  46. return ds;
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. FileHelper.Log(ex.Message, true);
  52. throw ex;
  53. }
  54. }
  55. else
  56. {
  57. throw new Exception("Unable to establish Connection to remote database.");
  58. }
  59. }
  60. }
  61. }
  62. }