OleDbConnection.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. IntPtr gdaConnection;
  22. #endregion
  23. #region Constructors
  24. public OleDbConnection ()
  25. {
  26. libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
  27. gdaConnection = IntPtr.Zero;
  28. connectionTimeout = 15;
  29. connectionString = null;
  30. }
  31. public OleDbConnection (string connectionString) : this ()
  32. {
  33. this.connectionString = connectionString;
  34. }
  35. #endregion // Constructors
  36. #region Properties
  37. public string ConnectionString {
  38. get {
  39. return connectionString;
  40. }
  41. set {
  42. connectionString = value;
  43. }
  44. }
  45. public int ConnectionTimeout {
  46. get {
  47. return connectionTimeout;
  48. }
  49. }
  50. public string Database {
  51. get {
  52. if (gdaConnection != IntPtr.Zero
  53. && libgda.gda_connection_is_open (gdaConnection)) {
  54. return libgda.gda_connection_get_database (gdaConnection);
  55. }
  56. return null;
  57. }
  58. }
  59. public string DataSource {
  60. get {
  61. if (gdaConnection != IntPtr.Zero
  62. && libgda.gda_connection_is_open (gdaConnection)) {
  63. return libgda.gda_connection_get_dsn (gdaConnection);
  64. }
  65. return null;
  66. }
  67. }
  68. public string Provider {
  69. get {
  70. if (gdaConnection != IntPtr.Zero
  71. && libgda.gda_connection_is_open (gdaConnection)) {
  72. return libgda.gda_connection_get_provider (gdaConnection);
  73. }
  74. return null;
  75. }
  76. }
  77. public string ServerVersion {
  78. get {
  79. if (gdaConnection != IntPtr.Zero
  80. && libgda.gda_connection_is_open (gdaConnection)) {
  81. return libgda.gda_connection_get_server_version (gdaConnection);
  82. }
  83. return null;
  84. }
  85. }
  86. public ConnectionState State
  87. {
  88. get {
  89. if (gdaConnection != IntPtr.Zero) {
  90. if (libgda.gda_connection_is_open (gdaConnection))
  91. return ConnectionState.Open;
  92. }
  93. return ConnectionState.Closed;
  94. }
  95. }
  96. internal IntPtr GdaConnection
  97. {
  98. get {
  99. return gdaConnection;
  100. }
  101. }
  102. #endregion // Properties
  103. #region Methods
  104. public OleDbTransaction BeginTransaction ()
  105. {
  106. if (gdaConnection != IntPtr.Zero)
  107. return new OleDbTransaction (this);
  108. return null;
  109. }
  110. IDbTransaction IDbConnection.BeginTransaction ()
  111. {
  112. return BeginTransaction ();
  113. }
  114. public OleDbTransaction BeginTransaction (IsolationLevel level)
  115. {
  116. if (gdaConnection != IntPtr.Zero)
  117. return new OleDbTransaction (this, level);
  118. return null;
  119. }
  120. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  121. {
  122. return BeginTransaction (level);
  123. }
  124. public void ChangeDatabase (string name)
  125. {
  126. if (gdaConnection == IntPtr.Zero)
  127. throw new ArgumentException ();
  128. if (State != ConnectionState.Open)
  129. throw new InvalidOperationException ();
  130. if (!libgda.gda_connection_change_database (gdaConnection, name))
  131. throw new OleDbException (this);
  132. }
  133. public void Close ()
  134. {
  135. if (State == ConnectionState.Open) {
  136. libgda.gda_connection_close (gdaConnection);
  137. gdaConnection = IntPtr.Zero;
  138. }
  139. }
  140. public OleDbCommand CreateCommand ()
  141. {
  142. if (State == ConnectionState.Open)
  143. return new OleDbCommand (null, this);
  144. return null;
  145. }
  146. [MonoTODO]
  147. protected override void Dispose (bool disposing)
  148. {
  149. throw new NotImplementedException ();
  150. }
  151. [MonoTODO]
  152. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  153. {
  154. throw new NotImplementedException ();
  155. }
  156. [MonoTODO]
  157. object ICloneable.Clone ()
  158. {
  159. throw new NotImplementedException();
  160. }
  161. IDbCommand IDbConnection.CreateCommand ()
  162. {
  163. return CreateCommand ();
  164. }
  165. public void Open ()
  166. {
  167. string provider = "Default";
  168. string gdaCncStr = "";
  169. string[] args;
  170. int len;
  171. char [] separator = { ';' };
  172. if (State == ConnectionState.Open)
  173. throw new InvalidOperationException ();
  174. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  175. connectionString,
  176. "", "", 0);
  177. /* convert the connection string to its GDA equivalent */
  178. //args = connectionString.Split (';');
  179. //len = args.Length;
  180. //for (int i = 0; i < len; i++) {
  181. // string[] values = args[i].Split (separator, 2);
  182. // if (values[0] == "Provider") {
  183. // if (values[1] == "SQLOLEDB")
  184. // provider = "FreeTDS";
  185. // else if (values[1] == "MSDAORA")
  186. // provider = "Oracle";
  187. // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
  188. // provider = "MS Access";
  189. // else
  190. // provider = values[2];
  191. // }
  192. // else if (values[0] == "Addr" || values[0] == "Address")
  193. // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
  194. // else if (values[0] == "Database")
  195. // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
  196. // else if (values[0] == "Connection Lifetime")
  197. // connectionTimeout = System.Convert.ToInt32 (values[1]);
  198. // else if (values[0] == "File Name")
  199. // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
  200. // else if (values[0] == "Password" || values[0] == "Pwd")
  201. // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
  202. // else if (values[0] == "User ID")
  203. // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
  204. //}
  205. /* open the connection */
  206. //System.Console.WriteLine ("Opening connection for provider " +
  207. // provider + " with " + gdaCncStr);
  208. //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
  209. // provider,
  210. // gdaCncStr);
  211. }
  212. [MonoTODO]
  213. public static void ReleaseObjectPool ()
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. #endregion
  218. #region Events and Delegates
  219. public event OleDbInfoMessageEventHandler InfoMessage;
  220. public event StateChangeEventHandler StateChange;
  221. #endregion
  222. }
  223. }