MsAccessDataConnection.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. using System.Data.Common;
  6. using System.Data.OleDb;
  7. using FastReport.Utils;
  8. namespace FastReport.Data
  9. {
  10. /// <summary>
  11. /// Represents a connection to MS Access database (.mdb file).
  12. /// </summary>
  13. /// <example>This example shows how to add a new connection to the report.
  14. /// <code>
  15. /// Report report1;
  16. /// MsAccessDataConnection conn = new MsAccessDataConnection();
  17. /// conn.DataSource = @"c:\data.mdb";
  18. /// report1.Dictionary.Connections.Add(conn);
  19. /// conn.CreateAllTables();
  20. /// </code>
  21. /// </example>
  22. public partial class MsAccessDataConnection : DataConnectionBase
  23. {
  24. internal static string strUserID = "User ID";
  25. internal static string strPassword = "Jet OLEDB:Database Password";
  26. /// <summary>
  27. /// Contains supported list of providers
  28. /// </summary>
  29. public static string[] ProviderList =
  30. {
  31. "Microsoft.Jet.OLEDB.4.0",
  32. "Microsoft.ACE.OLEDB.12.0"
  33. };
  34. /// <summary>
  35. /// Gets or sets the datasource file name.
  36. /// </summary>
  37. [Category("Data")]
  38. public string DataSource
  39. {
  40. get
  41. {
  42. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  43. return builder.DataSource;
  44. }
  45. set
  46. {
  47. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  48. builder.DataSource = value;
  49. ConnectionString = builder.ToString();
  50. }
  51. }
  52. /// <summary>
  53. /// Gets or sets the datasource file name.
  54. /// </summary>
  55. [Category("Data")]
  56. public string Provider
  57. {
  58. get
  59. {
  60. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  61. if (builder.Provider == "")
  62. return ProviderList[0];
  63. return builder.Provider;
  64. }
  65. set
  66. {
  67. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  68. builder.Provider = value;
  69. ConnectionString = builder.ToString();
  70. }
  71. }
  72. /// <summary>
  73. /// Gets or sets the user name.
  74. /// </summary>
  75. [Category("Data")]
  76. public string UserName
  77. {
  78. get
  79. {
  80. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  81. object userName;
  82. builder.TryGetValue(strUserID, out userName);
  83. return userName == null ? "" : userName.ToString();
  84. }
  85. set
  86. {
  87. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  88. if (!String.IsNullOrEmpty(value))
  89. builder.Add(strUserID, value);
  90. else
  91. builder.Remove(strUserID);
  92. ConnectionString = builder.ToString();
  93. }
  94. }
  95. /// <summary>
  96. /// Gets or sets the password.
  97. /// </summary>
  98. [Category("Data")]
  99. [PasswordPropertyText(true)]
  100. public string Password
  101. {
  102. get
  103. {
  104. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  105. object password;
  106. builder.TryGetValue(strPassword, out password);
  107. return password == null ? "" : password.ToString();
  108. }
  109. set
  110. {
  111. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  112. if (!String.IsNullOrEmpty(value))
  113. builder.Add(strPassword, value);
  114. else
  115. builder.Remove(strPassword);
  116. ConnectionString = builder.ToString();
  117. }
  118. }
  119. /// <inheritdoc/>
  120. protected override void SetConnectionString(string value)
  121. {
  122. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(value);
  123. base.SetConnectionString(builder.ToString());
  124. }
  125. /// <inheritdoc/>
  126. protected override string GetConnectionStringWithLoginInfo(string userName, string password)
  127. {
  128. OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(ConnectionString);
  129. builder.Remove(strUserID);
  130. builder.Add(strUserID, userName);
  131. builder.Remove(strPassword);
  132. builder.Add(strPassword, password);
  133. return builder.ToString();
  134. }
  135. /// <inheritdoc/>
  136. public override Type GetConnectionType()
  137. {
  138. return typeof(OleDbConnection);
  139. }
  140. /// <inheritdoc/>
  141. public override DbDataAdapter GetAdapter(string selectCommand, DbConnection connection,
  142. CommandParameterCollection parameters)
  143. {
  144. OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection as OleDbConnection);
  145. foreach (CommandParameter p in parameters)
  146. {
  147. OleDbParameter parameter = adapter.SelectCommand.Parameters.Add(p.Name, (OleDbType)p.DataType, p.Size);
  148. parameter.Value = p.Value;
  149. }
  150. return adapter;
  151. }
  152. /// <inheritdoc/>
  153. public override Type GetParameterType()
  154. {
  155. return typeof(OleDbType);
  156. }
  157. /// <inheritdoc/>
  158. public override string QuoteIdentifier(string value, DbConnection connection)
  159. {
  160. return "[" + value + "]";
  161. }
  162. /// <summary>
  163. /// Initializes a new instance of the <see cref="MsAccessDataConnection"/> class with default settings.
  164. /// </summary>
  165. public MsAccessDataConnection()
  166. {
  167. ConnectionString = "";
  168. }
  169. }
  170. }