DataConnectionBase.DesignExt.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using FastReport.Data.ConnectionEditors;
  2. using FastReport.Forms;
  3. using FastReport.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data.Common;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. namespace FastReport.Data
  11. {
  12. partial class DataConnectionBase : IHasEditor
  13. {
  14. internal DataSourceCloudInfo CloudInfo = new DataSourceCloudInfo();
  15. [Browsable(false)]
  16. [DefaultValue("")]
  17. public string CloudId { get; internal set; }
  18. #region Public Methods
  19. /// <inheritdoc/>
  20. public override void Delete()
  21. {
  22. Dispose();
  23. }
  24. /// <summary>
  25. /// Gets a string that will identify a connection in the Data Wizard.
  26. /// </summary>
  27. /// <returns>The string that contains the connection type and some meaningful information.</returns>
  28. public virtual string GetConnectionId()
  29. {
  30. return "";
  31. }
  32. /// <summary>
  33. /// Gets the default type for a new parameter.
  34. /// </summary>
  35. /// <returns>The integer representation of a parameter type.</returns>
  36. public virtual int GetDefaultParameterType()
  37. {
  38. return 0;
  39. }
  40. /// <summary>
  41. /// Gets a control that will be used to edit the connection properties.
  42. /// </summary>
  43. /// <returns>The editor's control.</returns>
  44. public virtual ConnectionEditorBase GetEditor()
  45. {
  46. return null;
  47. }
  48. /// <inheritdoc/>
  49. public bool InvokeEditor()
  50. {
  51. using (DataWizardForm form = new DataWizardForm(Report))
  52. {
  53. form.Connection = this;
  54. form.EditMode = true;
  55. return form.ShowDialog() == DialogResult.OK;
  56. }
  57. }
  58. /// <summary>
  59. /// Tests the connection.
  60. /// </summary>
  61. /// <remarks>
  62. /// If test connection is not successful, this method throws an exception. Catch this exception to
  63. /// show an error message.
  64. /// </remarks>
  65. public virtual void TestConnection()
  66. {
  67. DbConnection conn = GetConnection();
  68. if (conn != null)
  69. {
  70. try
  71. {
  72. OpenConnection(conn);
  73. }
  74. finally
  75. {
  76. DisposeConnection(conn);
  77. }
  78. }
  79. }
  80. partial void SetConnectionStringBrowsableAttribute()
  81. {
  82. // Get properties of the descriptor.
  83. PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(DataConnectionBase))["ConnectionString"];
  84. // Get "Browsable" attribute of the descriptor.
  85. BrowsableAttribute browsableAttribute = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
  86. FieldInfo browsable;
  87. browsable = browsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
  88. if (browsable == null)
  89. {
  90. browsable = browsableAttribute.GetType().GetField("<Browsable>k__BackingField", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
  91. }
  92. if (browsable != null)
  93. // Set "Browsable" attribute of the descriptor.
  94. browsable.SetValue(browsableAttribute, Config.ConnectionStringVisible);
  95. }
  96. #endregion Public Methods
  97. #region Internal Methods
  98. internal string GetQuotationChars()
  99. {
  100. DbConnection conn = GetConnection();
  101. try
  102. {
  103. OpenConnection(conn);
  104. return QuoteIdentifier("", conn);
  105. }
  106. finally
  107. {
  108. DisposeConnection(conn);
  109. }
  110. }
  111. #endregion Internal Methods
  112. #region Private Methods
  113. private void FilterTables(List<string> tableNames)
  114. {
  115. // filter tables
  116. for (int i = 0; i < tableNames.Count; i++)
  117. {
  118. Design.FilterConnectionTablesEventArgs e = new Design.FilterConnectionTablesEventArgs(this, tableNames[i]);
  119. Config.DesignerSettings.OnFilterConnectionTables(this, e);
  120. if (e.Skip)
  121. {
  122. tableNames.RemoveAt(i);
  123. i--;
  124. }
  125. }
  126. }
  127. private DbConnection GetDefaultConnection()
  128. {
  129. // if the ApplicationConnection is set, use it
  130. if (Config.DesignerSettings.ApplicationConnectionType == this.GetType())
  131. return Config.DesignerSettings.ApplicationConnection;
  132. return null;
  133. }
  134. private bool ShouldNotDispose(DbConnection connection)
  135. {
  136. // if this is the ApplicationConnection, do not dispose it
  137. return connection == Config.DesignerSettings.ApplicationConnection;
  138. }
  139. private void ShowLoginForm(string lastConnectionString)
  140. {
  141. if (String.IsNullOrEmpty(lastConnectionString))
  142. {
  143. using (AskLoginPasswordForm form = new AskLoginPasswordForm())
  144. {
  145. if (form.ShowDialog() == DialogResult.OK)
  146. lastConnectionString = GetConnectionStringWithLoginInfo(form.Login, form.Password);
  147. }
  148. }
  149. }
  150. #endregion Private Methods
  151. partial void SerializeDesignExt(FRWriter writer)
  152. {
  153. if (!string.IsNullOrEmpty(CloudId))
  154. {
  155. writer.WriteStr("CloudId", CloudId);
  156. }
  157. }
  158. internal class DataSourceCloudInfo
  159. {
  160. internal string Name { get; set; }
  161. internal string ConnectionString { get; set; }
  162. }
  163. }
  164. }