OleDbConnection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. string provider = "Default";
  181. string gdaCncStr = "";
  182. string[] args;
  183. int len;
  184. char [] separator = { ';' };
  185. if (State == ConnectionState.Open)
  186. throw new InvalidOperationException ();
  187. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  188. connectionString,
  189. "", "");
  190. /* convert the connection string to its GDA equivalent */
  191. //args = connectionString.Split (';');
  192. //len = args.Length;
  193. //for (int i = 0; i < len; i++) {
  194. // string[] values = args[i].Split (separator, 2);
  195. // if (values[0] == "Provider") {
  196. // if (values[1] == "SQLOLEDB")
  197. // provider = "FreeTDS";
  198. // else if (values[1] == "MSDAORA")
  199. // provider = "Oracle";
  200. // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
  201. // provider = "MS Access";
  202. // else
  203. // provider = values[2];
  204. // }
  205. // else if (values[0] == "Addr" || values[0] == "Address")
  206. // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
  207. // else if (values[0] == "Database")
  208. // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
  209. // else if (values[0] == "Connection Lifetime")
  210. // connectionTimeout = System.Convert.ToInt32 (values[1]);
  211. // else if (values[0] == "File Name")
  212. // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
  213. // else if (values[0] == "Password" || values[0] == "Pwd")
  214. // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
  215. // else if (values[0] == "User ID")
  216. // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
  217. //}
  218. /* open the connection */
  219. //System.Console.WriteLine ("Opening connection for provider " +
  220. // provider + " with " + gdaCncStr);
  221. //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
  222. // provider,
  223. // gdaCncStr);
  224. }
  225. [MonoTODO]
  226. public static void ReleaseObjectPool ()
  227. {
  228. throw new NotImplementedException ();
  229. }
  230. #endregion
  231. #region Events and Delegates
  232. public event OleDbInfoMessageEventHandler InfoMessage;
  233. public event StateChangeEventHandler StateChange;
  234. #endregion
  235. }
  236. }