DbConnConfigViewModel.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. using Aitex.UI.Charting.Command;
  7. using System.Windows.Forms;
  8. using Aitex.UI.Charting.View;
  9. using System.Configuration;
  10. namespace Aitex.UI.Charting.ViewModel
  11. {
  12. public class DbConnConfigViewModel : ChartingBaseViewModel
  13. {
  14. public DbConnConfigViewModel(DbConnConfig view)
  15. {
  16. _view = view;
  17. TestDbConnectionCommand = new ChartingCommand((o) => true, (o) => OnTestDbConnection(o));
  18. CloseCommand = new ChartingCommand((o) => true, (o) => CloseWin(o));
  19. Ip = RegEdit.ReadRegedit("Aitex", "Database_IP");
  20. if (string.IsNullOrEmpty(Ip))
  21. {
  22. //创建默认注册表
  23. RegEdit.WriteRegedit("Aitex", "Database_Ip", "127.0.0.1");
  24. RegEdit.WriteRegedit("Aitex", "Database_Port", "5432");
  25. RegEdit.WriteRegedit("Aitex", "Database_User", "postgres");
  26. RegEdit.WriteRegedit("Aitex", "Database_Pwd", "123456");
  27. RegEdit.WriteRegedit("Aitex", "Database_Name", "postgres");
  28. }
  29. Ip = RegEdit.ReadRegedit("Aitex", "Database_IP");
  30. Port = RegEdit.ReadRegedit("Aitex", "Database_Port");
  31. User = RegEdit.ReadRegedit("Aitex", "Database_User");
  32. Pwd = RegEdit.ReadRegedit("Aitex", "Database_Pwd");
  33. DbName = RegEdit.ReadRegedit("Aitex", "Database_Name");
  34. view.pwdbox.Password = Pwd;
  35. }
  36. private void OnTestDbConnection(object param)
  37. {
  38. Pwd = _view.pwdbox.Password; //for PasswordBox's 'Password' property do not support binding function
  39. RegEdit.WriteRegedit("Aitex", "Database_Ip", Ip);
  40. RegEdit.WriteRegedit("Aitex", "Database_Port", Port);
  41. RegEdit.WriteRegedit("Aitex", "Database_User", User);
  42. RegEdit.WriteRegedit("Aitex", "Database_Pwd", Pwd);
  43. RegEdit.WriteRegedit("Aitex", "Database_Name", DbName);
  44. string reason;
  45. if (CreateConnection(out reason))
  46. {
  47. MessageBox.Show(DataAnalysisControl.Properties.Resource.DbConnConfigViewModel_OnTestDbConnection_DBConnect_succeed, "Database", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
  48. _view.Close();
  49. }
  50. else
  51. {
  52. MessageBox.Show(DataAnalysisControl.Properties.Resource.DbConnConfigViewModel_OnTestDbConnection_db_failed + reason, "Database", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
  53. }
  54. }
  55. private void CloseWin(object param)
  56. {
  57. _view.Close();
  58. }
  59. DbConnConfig _view;
  60. public ICommand TestDbConnectionCommand { get; set; }
  61. public ICommand CloseCommand { get; set; }
  62. public string Ip { get; set; }
  63. public string Port { get; set; }
  64. public string User { get; set; }
  65. public string Pwd { get; set; }
  66. public string DbName { get; set; }
  67. }
  68. }