OleDbConnection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #if NET_2_0
  38. using System.Transactions;
  39. #endif
  40. namespace System.Data.OleDb
  41. {
  42. [DefaultEvent ("InfoMessage")]
  43. #if NET_2_0
  44. public sealed class OleDbConnection : DbConnection, ICloneable
  45. #else
  46. public sealed class OleDbConnection : Component, ICloneable, IDbConnection
  47. #endif
  48. {
  49. #region Fields
  50. string connectionString;
  51. int connectionTimeout;
  52. IntPtr gdaConnection;
  53. #endregion
  54. #region Constructors
  55. public OleDbConnection ()
  56. {
  57. gdaConnection = IntPtr.Zero;
  58. connectionTimeout = 15;
  59. }
  60. public OleDbConnection (string connectionString) : this ()
  61. {
  62. this.connectionString = connectionString;
  63. }
  64. #endregion // Constructors
  65. #region Properties
  66. [DataCategory ("Data")]
  67. [DefaultValue ("")]
  68. #if NET_1_0 || ONLY_1_1
  69. [DataSysDescriptionAttribute ("Information used to connect to a Data Source.")]
  70. #endif
  71. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  72. [RecommendedAsConfigurable (true)]
  73. [RefreshPropertiesAttribute (RefreshProperties.All)]
  74. public
  75. #if NET_2_0
  76. override
  77. #endif
  78. string ConnectionString {
  79. get {
  80. if (connectionString == null)
  81. return string.Empty;
  82. return connectionString;
  83. }
  84. set {
  85. connectionString = value;
  86. }
  87. }
  88. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  89. #if !NET_2_0
  90. [DataSysDescriptionAttribute ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")]
  91. #endif
  92. public
  93. #if NET_2_0
  94. override
  95. #endif
  96. int ConnectionTimeout {
  97. get {
  98. return connectionTimeout;
  99. }
  100. }
  101. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  102. #if !NET_2_0
  103. [DataSysDescriptionAttribute ("Current data source catalog value, 'Initial Catalog=X' in the connection string.")]
  104. #endif
  105. public
  106. #if NET_2_0
  107. override
  108. #endif
  109. string Database {
  110. get {
  111. if (gdaConnection != IntPtr.Zero
  112. && libgda.gda_connection_is_open (gdaConnection)) {
  113. return libgda.gda_connection_get_database (gdaConnection);
  114. }
  115. return string.Empty;
  116. }
  117. }
  118. #if NET_2_0
  119. [BrowsableAttribute (true)]
  120. #else
  121. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  122. [DataSysDescriptionAttribute ("Current data source, 'Data Source=X' in the connection string.")]
  123. #endif
  124. public
  125. #if NET_2_0
  126. override
  127. #endif
  128. string DataSource {
  129. get {
  130. if (gdaConnection != IntPtr.Zero
  131. && libgda.gda_connection_is_open (gdaConnection)) {
  132. return libgda.gda_connection_get_dsn (gdaConnection);
  133. }
  134. return string.Empty;
  135. }
  136. }
  137. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  138. #if NET_2_0
  139. [BrowsableAttribute (true)]
  140. #else
  141. [DataSysDescriptionAttribute ("Current OLE DB provider progid, 'Provider=X' in the connection string.")]
  142. #endif
  143. public string Provider {
  144. get {
  145. if (gdaConnection != IntPtr.Zero
  146. && libgda.gda_connection_is_open (gdaConnection)) {
  147. return libgda.gda_connection_get_provider (gdaConnection);
  148. }
  149. return string.Empty;
  150. }
  151. }
  152. #if !NET_2_0
  153. [DataSysDescriptionAttribute ("Version of the product accessed by the OLE DB Provider.")]
  154. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  155. [BrowsableAttribute (false)]
  156. #endif
  157. public
  158. #if NET_2_0
  159. override
  160. #endif
  161. string ServerVersion {
  162. get {
  163. if (State == ConnectionState.Closed)
  164. throw ExceptionHelper.ConnectionClosed ();
  165. return libgda.gda_connection_get_server_version (gdaConnection);
  166. }
  167. }
  168. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  169. #if !NET_2_0
  170. [DataSysDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed.")]
  171. #endif
  172. [BrowsableAttribute (false)]
  173. public
  174. #if NET_2_0
  175. override
  176. #endif
  177. ConnectionState State {
  178. get {
  179. if (gdaConnection != IntPtr.Zero) {
  180. if (libgda.gda_connection_is_open (gdaConnection))
  181. return ConnectionState.Open;
  182. }
  183. return ConnectionState.Closed;
  184. }
  185. }
  186. internal IntPtr GdaConnection {
  187. get {
  188. return gdaConnection;
  189. }
  190. }
  191. #endregion // Properties
  192. #region Methods
  193. public new OleDbTransaction BeginTransaction ()
  194. {
  195. if (State == ConnectionState.Closed)
  196. throw ExceptionHelper.ConnectionClosed ();
  197. return new OleDbTransaction (this);
  198. }
  199. public new OleDbTransaction BeginTransaction (IsolationLevel level)
  200. {
  201. if (State == ConnectionState.Closed)
  202. throw ExceptionHelper.ConnectionClosed ();
  203. return new OleDbTransaction (this, level);
  204. }
  205. #if NET_2_0
  206. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
  207. {
  208. return BeginTransaction (isolationLevel);
  209. }
  210. protected override DbCommand CreateDbCommand()
  211. {
  212. return CreateCommand ();
  213. }
  214. #else
  215. IDbTransaction IDbConnection.BeginTransaction ()
  216. {
  217. return BeginTransaction ();
  218. }
  219. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  220. {
  221. return BeginTransaction (level);
  222. }
  223. IDbCommand IDbConnection.CreateCommand ()
  224. {
  225. return CreateCommand ();
  226. }
  227. #endif
  228. public
  229. #if NET_2_0
  230. override
  231. #endif
  232. void ChangeDatabase (string name)
  233. {
  234. if (State != ConnectionState.Open)
  235. throw new InvalidOperationException ();
  236. if (!libgda.gda_connection_change_database (gdaConnection, name))
  237. throw new OleDbException (this);
  238. }
  239. public
  240. #if NET_2_0
  241. override
  242. #endif
  243. void Close ()
  244. {
  245. if (State == ConnectionState.Open) {
  246. libgda.gda_connection_close (gdaConnection);
  247. gdaConnection = IntPtr.Zero;
  248. }
  249. }
  250. public new OleDbCommand CreateCommand ()
  251. {
  252. if (State == ConnectionState.Open)
  253. return new OleDbCommand (null, this);
  254. return null;
  255. }
  256. [MonoTODO]
  257. protected override void Dispose (bool disposing)
  258. {
  259. throw new NotImplementedException ();
  260. }
  261. [MonoTODO]
  262. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. [MonoTODO]
  267. object ICloneable.Clone ()
  268. {
  269. throw new NotImplementedException();
  270. }
  271. public
  272. #if NET_2_0
  273. override
  274. #endif
  275. void Open ()
  276. {
  277. string provider = "Default";
  278. string gdaCncStr = string.Empty;
  279. string[] args;
  280. int len;
  281. char [] separator = { ';' };
  282. if (State == ConnectionState.Open)
  283. throw new InvalidOperationException ();
  284. libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
  285. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  286. ConnectionString, string.Empty, string.Empty, 0);
  287. if (gdaConnection == IntPtr.Zero)
  288. throw new OleDbException (this);
  289. /* convert the connection string to its GDA equivalent */
  290. //args = connectionString.Split (';');
  291. //len = args.Length;
  292. //for (int i = 0; i < len; i++) {
  293. // string[] values = args[i].Split (separator, 2);
  294. // if (values[0] == "Provider") {
  295. // if (values[1] == "SQLOLEDB")
  296. // provider = "FreeTDS";
  297. // else if (values[1] == "MSDAORA")
  298. // provider = "Oracle";
  299. // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
  300. // provider = "MS Access";
  301. // else
  302. // provider = values[2];
  303. // }
  304. // else if (values[0] == "Addr" || values[0] == "Address")
  305. // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
  306. // else if (values[0] == "Database")
  307. // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
  308. // else if (values[0] == "Connection Lifetime")
  309. // connectionTimeout = System.Convert.ToInt32 (values[1]);
  310. // else if (values[0] == "File Name")
  311. // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
  312. // else if (values[0] == "Password" || values[0] == "Pwd")
  313. // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
  314. // else if (values[0] == "User ID")
  315. // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
  316. //}
  317. /* open the connection */
  318. //System.Console.WriteLine ("Opening connection for provider " +
  319. // provider + " with " + gdaCncStr);
  320. //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
  321. // provider,
  322. // gdaCncStr);
  323. }
  324. [MonoTODO]
  325. public static void ReleaseObjectPool ()
  326. {
  327. throw new NotImplementedException ();
  328. }
  329. [MonoTODO]
  330. public void EnlistDistributedTransaction (ITransaction transaction)
  331. {
  332. throw new NotImplementedException ();
  333. }
  334. #if NET_2_0
  335. [MonoTODO]
  336. public override void EnlistTransaction (Transaction transaction)
  337. {
  338. throw new NotImplementedException ();
  339. }
  340. [MonoTODO]
  341. public override DataTable GetSchema ()
  342. {
  343. if (State == ConnectionState.Closed)
  344. throw ExceptionHelper.ConnectionClosed ();
  345. throw new NotImplementedException ();
  346. }
  347. [MonoTODO]
  348. public override DataTable GetSchema(string collectionName)
  349. {
  350. return GetSchema (collectionName, null);
  351. }
  352. [MonoTODO]
  353. public override DataTable GetSchema (String collectionName, string [] restrictionValues)
  354. {
  355. if (State == ConnectionState.Closed)
  356. throw ExceptionHelper.ConnectionClosed ();
  357. throw new NotImplementedException ();
  358. }
  359. [MonoTODO]
  360. [EditorBrowsable (EditorBrowsableState.Advanced)]
  361. public void ResetState()
  362. {
  363. throw new NotImplementedException ();
  364. }
  365. #endif
  366. #endregion
  367. #region Events and Delegates
  368. #if !NET_2_0
  369. [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
  370. #endif
  371. [DataCategory ("DataCategory_InfoMessage")]
  372. public event OleDbInfoMessageEventHandler InfoMessage;
  373. #if !NET_2_0
  374. [DataSysDescription ("Event triggered when the connection changes state.")]
  375. [DataCategory ("DataCategory_StateChange")]
  376. public event StateChangeEventHandler StateChange;
  377. #endif
  378. #endregion
  379. }
  380. }