OleDbConnection.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. using System.EnterpriseServices;
  15. namespace System.Data.OleDb
  16. {
  17. [DefaultEvent ("InfoMessage")]
  18. public sealed class OleDbConnection : Component, ICloneable, IDbConnection
  19. {
  20. #region Fields
  21. string connectionString;
  22. int connectionTimeout;
  23. IntPtr gdaConnection;
  24. #endregion
  25. #region Constructors
  26. public OleDbConnection ()
  27. {
  28. libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
  29. gdaConnection = IntPtr.Zero;
  30. connectionTimeout = 15;
  31. connectionString = null;
  32. }
  33. public OleDbConnection (string connectionString) : this ()
  34. {
  35. this.connectionString = connectionString;
  36. }
  37. #endregion // Constructors
  38. #region Properties
  39. [DataCategory ("Data")]
  40. [DefaultValue ("")]
  41. [DataSysDescriptionAttribute ("Information used to connect to a Data Source")]
  42. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  43. [RecommendedAsConfigurableAttribute (true)]
  44. [RefreshPropertiesAttribute (RefreshProperties.All)]
  45. public string ConnectionString {
  46. get {
  47. return connectionString;
  48. }
  49. set {
  50. connectionString = value;
  51. }
  52. }
  53. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  54. [DataSysDescriptionAttribute ("Current connection timeout value 'Connect TimeOut=X' in the ConnectionString")]
  55. public int ConnectionTimeout {
  56. get {
  57. return connectionTimeout;
  58. }
  59. }
  60. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  61. [DataSysDescriptionAttribute ("Current data source Catlog value, 'Initial Catalog=X' in the ConnectionString")]
  62. public string Database {
  63. get {
  64. if (gdaConnection != IntPtr.Zero
  65. && libgda.gda_connection_is_open (gdaConnection)) {
  66. return libgda.gda_connection_get_database (gdaConnection);
  67. }
  68. return null;
  69. }
  70. }
  71. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  72. [DataSysDescriptionAttribute ("Current data source, 'Data Source=X' in the ConnectionString")]
  73. public string DataSource {
  74. get {
  75. if (gdaConnection != IntPtr.Zero
  76. && libgda.gda_connection_is_open (gdaConnection)) {
  77. return libgda.gda_connection_get_dsn (gdaConnection);
  78. }
  79. return null;
  80. }
  81. }
  82. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  83. [DataSysDescriptionAttribute ("Current OLE DB provider progid, 'Provider=X' in the ConnectionString")]
  84. public string Provider {
  85. get {
  86. if (gdaConnection != IntPtr.Zero
  87. && libgda.gda_connection_is_open (gdaConnection)) {
  88. return libgda.gda_connection_get_provider (gdaConnection);
  89. }
  90. return null;
  91. }
  92. }
  93. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  94. [DataSysDescriptionAttribute ("Version of the product accessed by the OLE DB Provider")]
  95. [BrowsableAttribute (false)]
  96. public string ServerVersion {
  97. get {
  98. if (gdaConnection != IntPtr.Zero
  99. && libgda.gda_connection_is_open (gdaConnection)) {
  100. return libgda.gda_connection_get_server_version (gdaConnection);
  101. }
  102. return null;
  103. }
  104. }
  105. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  106. [DataSysDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed")]
  107. [BrowsableAttribute (false)]
  108. public ConnectionState State
  109. {
  110. get {
  111. if (gdaConnection != IntPtr.Zero) {
  112. if (libgda.gda_connection_is_open (gdaConnection))
  113. return ConnectionState.Open;
  114. }
  115. return ConnectionState.Closed;
  116. }
  117. }
  118. internal IntPtr GdaConnection
  119. {
  120. get {
  121. return gdaConnection;
  122. }
  123. }
  124. #endregion // Properties
  125. #region Methods
  126. public OleDbTransaction BeginTransaction ()
  127. {
  128. if (gdaConnection != IntPtr.Zero)
  129. return new OleDbTransaction (this);
  130. return null;
  131. }
  132. IDbTransaction IDbConnection.BeginTransaction ()
  133. {
  134. return BeginTransaction ();
  135. }
  136. public OleDbTransaction BeginTransaction (IsolationLevel level)
  137. {
  138. if (gdaConnection != IntPtr.Zero)
  139. return new OleDbTransaction (this, level);
  140. return null;
  141. }
  142. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  143. {
  144. return BeginTransaction (level);
  145. }
  146. public void ChangeDatabase (string name)
  147. {
  148. if (gdaConnection == IntPtr.Zero)
  149. throw new ArgumentException ();
  150. if (State != ConnectionState.Open)
  151. throw new InvalidOperationException ();
  152. if (!libgda.gda_connection_change_database (gdaConnection, name))
  153. throw new OleDbException (this);
  154. }
  155. public void Close ()
  156. {
  157. if (State == ConnectionState.Open) {
  158. libgda.gda_connection_close (gdaConnection);
  159. gdaConnection = IntPtr.Zero;
  160. }
  161. }
  162. public OleDbCommand CreateCommand ()
  163. {
  164. if (State == ConnectionState.Open)
  165. return new OleDbCommand (null, this);
  166. return null;
  167. }
  168. [MonoTODO]
  169. protected override void Dispose (bool disposing)
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. [MonoTODO]
  174. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. [MonoTODO]
  179. object ICloneable.Clone ()
  180. {
  181. throw new NotImplementedException();
  182. }
  183. IDbCommand IDbConnection.CreateCommand ()
  184. {
  185. return CreateCommand ();
  186. }
  187. public void Open ()
  188. {
  189. string provider = "Default";
  190. string gdaCncStr = "";
  191. string[] args;
  192. int len;
  193. char [] separator = { ';' };
  194. if (State == ConnectionState.Open)
  195. throw new InvalidOperationException ();
  196. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  197. connectionString,
  198. "", "", 0);
  199. /* convert the connection string to its GDA equivalent */
  200. //args = connectionString.Split (';');
  201. //len = args.Length;
  202. //for (int i = 0; i < len; i++) {
  203. // string[] values = args[i].Split (separator, 2);
  204. // if (values[0] == "Provider") {
  205. // if (values[1] == "SQLOLEDB")
  206. // provider = "FreeTDS";
  207. // else if (values[1] == "MSDAORA")
  208. // provider = "Oracle";
  209. // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
  210. // provider = "MS Access";
  211. // else
  212. // provider = values[2];
  213. // }
  214. // else if (values[0] == "Addr" || values[0] == "Address")
  215. // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
  216. // else if (values[0] == "Database")
  217. // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
  218. // else if (values[0] == "Connection Lifetime")
  219. // connectionTimeout = System.Convert.ToInt32 (values[1]);
  220. // else if (values[0] == "File Name")
  221. // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
  222. // else if (values[0] == "Password" || values[0] == "Pwd")
  223. // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
  224. // else if (values[0] == "User ID")
  225. // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
  226. //}
  227. /* open the connection */
  228. //System.Console.WriteLine ("Opening connection for provider " +
  229. // provider + " with " + gdaCncStr);
  230. //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
  231. // provider,
  232. // gdaCncStr);
  233. }
  234. [MonoTODO]
  235. public static void ReleaseObjectPool ()
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. [MonoTODO]
  240. public void EnlistDistributedTransaction (ITransaction transaction)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. #endregion
  245. #region Events and Delegates
  246. [DataSysDescription ("DbConnection_InfoMessage")]
  247. [DataCategory ("DataCategory_InfoMessage")]
  248. public event OleDbInfoMessageEventHandler InfoMessage;
  249. [DataSysDescription ("DbConnection_StateChange")]
  250. [DataCategory ("DataCategory_StateChange")]
  251. public event StateChangeEventHandler StateChange;
  252. #endregion
  253. }
  254. }