DataConnectionBase.DesignExt.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using FastReport.Data.ConnectionEditors;
  2. using FastReport.Forms;
  3. using FastReport.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.Common;
  7. using System.Windows.Forms;
  8. namespace FastReport.Data
  9. {
  10. partial class DataConnectionBase : IHasEditor
  11. {
  12. #region Public Methods
  13. /// <inheritdoc/>
  14. public override void Delete()
  15. {
  16. Dispose();
  17. }
  18. /// <summary>
  19. /// Gets a string that will identify a connection in the Data Wizard.
  20. /// </summary>
  21. /// <returns>The string that contains the connection type and some meaningful information.</returns>
  22. public virtual string GetConnectionId()
  23. {
  24. return "";
  25. }
  26. /// <summary>
  27. /// Gets the default type for a new parameter.
  28. /// </summary>
  29. /// <returns>The integer representation of a parameter type.</returns>
  30. public virtual int GetDefaultParameterType()
  31. {
  32. return 0;
  33. }
  34. /// <summary>
  35. /// Gets a control that will be used to edit the connection properties.
  36. /// </summary>
  37. /// <returns>The editor's control.</returns>
  38. public virtual ConnectionEditorBase GetEditor()
  39. {
  40. return null;
  41. }
  42. /// <inheritdoc/>
  43. public bool InvokeEditor()
  44. {
  45. using (DataWizardForm form = new DataWizardForm(Report))
  46. {
  47. form.Connection = this;
  48. form.EditMode = true;
  49. return form.ShowDialog() == DialogResult.OK;
  50. }
  51. }
  52. /// <summary>
  53. /// Tests the connection.
  54. /// </summary>
  55. /// <remarks>
  56. /// If test connection is not successful, this method throws an exception. Catch this exception to
  57. /// show an error message.
  58. /// </remarks>
  59. public virtual void TestConnection()
  60. {
  61. DbConnection conn = GetConnection();
  62. if (conn != null)
  63. {
  64. try
  65. {
  66. OpenConnection(conn);
  67. }
  68. finally
  69. {
  70. DisposeConnection(conn);
  71. }
  72. }
  73. }
  74. #endregion Public Methods
  75. #region Internal Methods
  76. internal string GetQuotationChars()
  77. {
  78. DbConnection conn = GetConnection();
  79. try
  80. {
  81. OpenConnection(conn);
  82. return QuoteIdentifier("", conn);
  83. }
  84. finally
  85. {
  86. DisposeConnection(conn);
  87. }
  88. }
  89. #endregion Internal Methods
  90. #region Private Methods
  91. private void FilterTables(List<string> tableNames)
  92. {
  93. // filter tables
  94. for (int i = 0; i < tableNames.Count; i++)
  95. {
  96. Design.FilterConnectionTablesEventArgs e = new Design.FilterConnectionTablesEventArgs(this, tableNames[i]);
  97. Config.DesignerSettings.OnFilterConnectionTables(this, e);
  98. if (e.Skip)
  99. {
  100. tableNames.RemoveAt(i);
  101. i--;
  102. }
  103. }
  104. }
  105. private DbConnection GetDefaultConnection()
  106. {
  107. // if the ApplicationConnection is set, use it
  108. if (Config.DesignerSettings.ApplicationConnectionType == this.GetType())
  109. return Config.DesignerSettings.ApplicationConnection;
  110. return null;
  111. }
  112. private bool ShouldNotDispose(DbConnection connection)
  113. {
  114. // if this is the ApplicationConnection, do not dispose it
  115. return connection == Config.DesignerSettings.ApplicationConnection;
  116. }
  117. private void ShowLoginForm(string lastConnectionString)
  118. {
  119. if (String.IsNullOrEmpty(lastConnectionString))
  120. {
  121. using (AskLoginPasswordForm form = new AskLoginPasswordForm())
  122. {
  123. if (form.ShowDialog() == DialogResult.OK)
  124. lastConnectionString = GetConnectionStringWithLoginInfo(form.Login, form.Password);
  125. }
  126. }
  127. }
  128. #endregion Private Methods
  129. }
  130. }