OleDbConnection.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.ComponentModel;
  34. using System.Data;
  35. using System.Data.Common;
  36. using System.EnterpriseServices;
  37. namespace System.Data.OleDb
  38. {
  39. [DefaultEvent ("InfoMessage")]
  40. public sealed class OleDbConnection : Component, ICloneable, IDbConnection
  41. {
  42. #region Fields
  43. string connectionString;
  44. int connectionTimeout;
  45. IntPtr gdaConnection;
  46. #endregion
  47. #region Constructors
  48. public OleDbConnection ()
  49. {
  50. libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
  51. gdaConnection = IntPtr.Zero;
  52. connectionTimeout = 15;
  53. connectionString = null;
  54. }
  55. public OleDbConnection (string connectionString) : this ()
  56. {
  57. this.connectionString = connectionString;
  58. }
  59. #endregion // Constructors
  60. #region Properties
  61. [DataCategory ("Data")]
  62. [DefaultValue ("")]
  63. #if !NET_2_0
  64. [DataSysDescriptionAttribute ("Information used to connect to a Data Source.")]
  65. #endif
  66. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  67. #if NET_2_0
  68. [SettingsBindableAttribute (true)]
  69. #else
  70. [RecommendedAsConfigurableAttribute (true)]
  71. #endif
  72. [RefreshPropertiesAttribute (RefreshProperties.All)]
  73. public string ConnectionString {
  74. get {
  75. return connectionString;
  76. }
  77. set {
  78. connectionString = value;
  79. }
  80. }
  81. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  82. #if !NET_2_0
  83. [DataSysDescriptionAttribute ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")]
  84. #endif
  85. public int ConnectionTimeout {
  86. get {
  87. return connectionTimeout;
  88. }
  89. }
  90. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  91. #if !NET_2_0
  92. [DataSysDescriptionAttribute ("Current data source catalog value, 'Initial Catalog=X' in the connection string.")]
  93. #endif
  94. public string Database {
  95. get {
  96. if (gdaConnection != IntPtr.Zero
  97. && libgda.gda_connection_is_open (gdaConnection)) {
  98. return libgda.gda_connection_get_database (gdaConnection);
  99. }
  100. return null;
  101. }
  102. }
  103. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  104. #if !NET_2_0
  105. [DataSysDescriptionAttribute ("Current data source, 'Data Source=X' in the connection string.")]
  106. #endif
  107. public string DataSource {
  108. get {
  109. if (gdaConnection != IntPtr.Zero
  110. && libgda.gda_connection_is_open (gdaConnection)) {
  111. return libgda.gda_connection_get_dsn (gdaConnection);
  112. }
  113. return null;
  114. }
  115. }
  116. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  117. #if !NET_2_0
  118. [DataSysDescriptionAttribute ("Current OLE DB provider progid, 'Provider=X' in the connection string.")]
  119. #endif
  120. public string Provider {
  121. get {
  122. if (gdaConnection != IntPtr.Zero
  123. && libgda.gda_connection_is_open (gdaConnection)) {
  124. return libgda.gda_connection_get_provider (gdaConnection);
  125. }
  126. return null;
  127. }
  128. }
  129. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  130. #if !NET_2_0
  131. [DataSysDescriptionAttribute ("Version of the product accessed by the OLE DB Provider.")]
  132. #endif
  133. [BrowsableAttribute (false)]
  134. public string ServerVersion {
  135. get {
  136. if (gdaConnection != IntPtr.Zero
  137. && libgda.gda_connection_is_open (gdaConnection)) {
  138. return libgda.gda_connection_get_server_version (gdaConnection);
  139. }
  140. return null;
  141. }
  142. }
  143. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  144. #if !NET_2_0
  145. [DataSysDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed.")]
  146. #endif
  147. [BrowsableAttribute (false)]
  148. public ConnectionState State
  149. {
  150. get {
  151. if (gdaConnection != IntPtr.Zero) {
  152. if (libgda.gda_connection_is_open (gdaConnection))
  153. return ConnectionState.Open;
  154. }
  155. return ConnectionState.Closed;
  156. }
  157. }
  158. internal IntPtr GdaConnection
  159. {
  160. get {
  161. return gdaConnection;
  162. }
  163. }
  164. #endregion // Properties
  165. #region Methods
  166. public OleDbTransaction BeginTransaction ()
  167. {
  168. if (gdaConnection != IntPtr.Zero)
  169. return new OleDbTransaction (this);
  170. return null;
  171. }
  172. IDbTransaction IDbConnection.BeginTransaction ()
  173. {
  174. return BeginTransaction ();
  175. }
  176. public OleDbTransaction BeginTransaction (IsolationLevel level)
  177. {
  178. if (gdaConnection != IntPtr.Zero)
  179. return new OleDbTransaction (this, level);
  180. return null;
  181. }
  182. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  183. {
  184. return BeginTransaction (level);
  185. }
  186. public void ChangeDatabase (string name)
  187. {
  188. if (gdaConnection == IntPtr.Zero)
  189. throw new ArgumentException ();
  190. if (State != ConnectionState.Open)
  191. throw new InvalidOperationException ();
  192. if (!libgda.gda_connection_change_database (gdaConnection, name))
  193. throw new OleDbException (this);
  194. }
  195. public void Close ()
  196. {
  197. if (State == ConnectionState.Open) {
  198. libgda.gda_connection_close (gdaConnection);
  199. gdaConnection = IntPtr.Zero;
  200. }
  201. }
  202. public OleDbCommand CreateCommand ()
  203. {
  204. if (State == ConnectionState.Open)
  205. return new OleDbCommand (null, this);
  206. return null;
  207. }
  208. [MonoTODO]
  209. protected override void Dispose (bool disposing)
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. [MonoTODO]
  214. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  215. {
  216. throw new NotImplementedException ();
  217. }
  218. [MonoTODO]
  219. object ICloneable.Clone ()
  220. {
  221. throw new NotImplementedException();
  222. }
  223. IDbCommand IDbConnection.CreateCommand ()
  224. {
  225. return CreateCommand ();
  226. }
  227. public void Open ()
  228. {
  229. string provider = "Default";
  230. string gdaCncStr = "";
  231. string[] args;
  232. int len;
  233. char [] separator = { ';' };
  234. if (State == ConnectionState.Open)
  235. throw new InvalidOperationException ();
  236. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  237. connectionString,
  238. "", "", 0);
  239. if (gdaConnection==IntPtr.Zero)
  240. throw new OleDbException (this);
  241. /* convert the connection string to its GDA equivalent */
  242. //args = connectionString.Split (';');
  243. //len = args.Length;
  244. //for (int i = 0; i < len; i++) {
  245. // string[] values = args[i].Split (separator, 2);
  246. // if (values[0] == "Provider") {
  247. // if (values[1] == "SQLOLEDB")
  248. // provider = "FreeTDS";
  249. // else if (values[1] == "MSDAORA")
  250. // provider = "Oracle";
  251. // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
  252. // provider = "MS Access";
  253. // else
  254. // provider = values[2];
  255. // }
  256. // else if (values[0] == "Addr" || values[0] == "Address")
  257. // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
  258. // else if (values[0] == "Database")
  259. // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
  260. // else if (values[0] == "Connection Lifetime")
  261. // connectionTimeout = System.Convert.ToInt32 (values[1]);
  262. // else if (values[0] == "File Name")
  263. // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
  264. // else if (values[0] == "Password" || values[0] == "Pwd")
  265. // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
  266. // else if (values[0] == "User ID")
  267. // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
  268. //}
  269. /* open the connection */
  270. //System.Console.WriteLine ("Opening connection for provider " +
  271. // provider + " with " + gdaCncStr);
  272. //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
  273. // provider,
  274. // gdaCncStr);
  275. }
  276. [MonoTODO]
  277. public static void ReleaseObjectPool ()
  278. {
  279. throw new NotImplementedException ();
  280. }
  281. [MonoTODO]
  282. public void EnlistDistributedTransaction (ITransaction transaction)
  283. {
  284. throw new NotImplementedException ();
  285. }
  286. #endregion
  287. #region Events and Delegates
  288. #if !NET_2_0
  289. [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
  290. #endif
  291. [DataCategory ("DataCategory_InfoMessage")]
  292. public event OleDbInfoMessageEventHandler InfoMessage;
  293. #if !NET_2_0
  294. [DataSysDescription ("Event triggered when the connection changes state.")]
  295. #endif
  296. [DataCategory ("DataCategory_StateChange")]
  297. public event StateChangeEventHandler StateChange;
  298. #endregion
  299. }
  300. }