OleDbConnection.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // System.Data.OleDb.OleDbConnection
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Rodrigo Moya, 2002
  9. // Copyright (C) Tim Coleman, 2002
  10. //
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Common;
  14. namespace System.Data.OleDb
  15. {
  16. public sealed class OleDbConnection : Component, ICloneable, IDbConnection
  17. {
  18. #region Fields
  19. string connectionString;
  20. int connectionTimeout;
  21. OleDbDataReader dataReader;
  22. IntPtr gdaConnection;
  23. #endregion
  24. #region Constructors
  25. public OleDbConnection ()
  26. {
  27. libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
  28. gdaConnection = IntPtr.Zero;
  29. connectionTimeout = 15;
  30. connectionString = null;
  31. dataReader = null;
  32. }
  33. public OleDbConnection (string connectionString) : this ()
  34. {
  35. this.connectionString = connectionString;
  36. }
  37. #endregion // Constructors
  38. #region Properties
  39. public string ConnectionString {
  40. get {
  41. return connectionString;
  42. }
  43. set {
  44. connectionString = value;
  45. }
  46. }
  47. public int ConnectionTimeout {
  48. get {
  49. return connectionTimeout;
  50. }
  51. }
  52. public string Database {
  53. get {
  54. if (gdaConnection != IntPtr.Zero
  55. && libgda.gda_connection_is_open (gdaConnection)) {
  56. return libgda.gda_connection_get_database (gdaConnection);
  57. }
  58. return null;
  59. }
  60. }
  61. public string DataSource {
  62. get {
  63. if (gdaConnection != IntPtr.Zero
  64. && libgda.gda_connection_is_open (gdaConnection)) {
  65. return libgda.gda_connection_get_dsn (gdaConnection);
  66. }
  67. return null;
  68. }
  69. }
  70. public string Provider {
  71. get {
  72. if (gdaConnection != IntPtr.Zero
  73. && libgda.gda_connection_is_open (gdaConnection)) {
  74. return libgda.gda_connection_get_provider (gdaConnection);
  75. }
  76. return null;
  77. }
  78. }
  79. public string ServerVersion {
  80. get {
  81. if (gdaConnection != IntPtr.Zero
  82. && libgda.gda_connection_is_open (gdaConnection)) {
  83. return libgda.gda_connection_get_server_version (gdaConnection);
  84. }
  85. return null;
  86. }
  87. }
  88. public ConnectionState State
  89. {
  90. get {
  91. if (gdaConnection != IntPtr.Zero) {
  92. if (libgda.gda_connection_is_open (gdaConnection))
  93. return ConnectionState.Open;
  94. }
  95. return ConnectionState.Closed;
  96. }
  97. }
  98. internal IntPtr GdaConnection
  99. {
  100. get {
  101. return gdaConnection;
  102. }
  103. }
  104. internal OleDbDataReader DataReader
  105. {
  106. get {
  107. return dataReader;
  108. }
  109. set {
  110. dataReader = value;
  111. }
  112. }
  113. #endregion // Properties
  114. #region Methods
  115. public OleDbTransaction BeginTransaction ()
  116. {
  117. if (gdaConnection != IntPtr.Zero)
  118. return new OleDbTransaction (this);
  119. return null;
  120. }
  121. IDbTransaction IDbConnection.BeginTransaction ()
  122. {
  123. return BeginTransaction ();
  124. }
  125. public OleDbTransaction BeginTransaction (IsolationLevel level)
  126. {
  127. if (gdaConnection != IntPtr.Zero)
  128. return new OleDbTransaction (this, level);
  129. return null;
  130. }
  131. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  132. {
  133. return BeginTransaction (level);
  134. }
  135. public void ChangeDatabase (string name)
  136. {
  137. if (gdaConnection == IntPtr.Zero)
  138. throw new ArgumentException ();
  139. if (State != ConnectionState.Open)
  140. throw new InvalidOperationException ();
  141. if (!libgda.gda_connection_change_database (gdaConnection, name))
  142. throw new OleDbException (this);
  143. }
  144. public void Close ()
  145. {
  146. if (State == ConnectionState.Open) {
  147. libgda.gda_connection_close (gdaConnection);
  148. gdaConnection = IntPtr.Zero;
  149. }
  150. dataReader = null;
  151. }
  152. public OleDbCommand CreateCommand ()
  153. {
  154. if (State == ConnectionState.Open &&
  155. DataReader == null)
  156. return new OleDbCommand (null, this);
  157. return null;
  158. }
  159. [MonoTODO]
  160. protected override void Dispose (bool disposing)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. [MonoTODO]
  165. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  166. {
  167. throw new NotImplementedException ();
  168. }
  169. [MonoTODO]
  170. object ICloneable.Clone ()
  171. {
  172. throw new NotImplementedException();
  173. }
  174. IDbCommand IDbConnection.CreateCommand ()
  175. {
  176. return CreateCommand ();
  177. }
  178. public void Open ()
  179. {
  180. if (State == ConnectionState.Open)
  181. throw new InvalidOperationException ();
  182. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  183. connectionString,
  184. "", "");
  185. }
  186. [MonoTODO]
  187. public static void ReleaseObjectPool ()
  188. {
  189. throw new NotImplementedException ();
  190. }
  191. #endregion
  192. #region Events and Delegates
  193. public event OleDbInfoMessageEventHandler InfoMessage;
  194. public event StateChangeEventHandler StateChange;
  195. #endregion
  196. }
  197. }