OleDbConnection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
  58. gdaConnection = IntPtr.Zero;
  59. connectionTimeout = 15;
  60. connectionString = null;
  61. }
  62. public OleDbConnection (string connectionString) : this ()
  63. {
  64. this.connectionString = connectionString;
  65. }
  66. #endregion // Constructors
  67. #region Properties
  68. [DataCategory ("Data")]
  69. [DefaultValue ("")]
  70. #if NET_1_0 || ONLY_1_1
  71. [DataSysDescriptionAttribute ("Information used to connect to a Data Source.")]
  72. #endif
  73. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  74. [RecommendedAsConfigurable (true)]
  75. [RefreshPropertiesAttribute (RefreshProperties.All)]
  76. public
  77. #if NET_2_0
  78. override
  79. #endif
  80. string ConnectionString {
  81. get {
  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 null;
  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 null;
  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 null;
  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 (gdaConnection != IntPtr.Zero
  164. && libgda.gda_connection_is_open (gdaConnection)) {
  165. return libgda.gda_connection_get_server_version (gdaConnection);
  166. }
  167. return null;
  168. }
  169. }
  170. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  171. #if !NET_2_0
  172. [DataSysDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed.")]
  173. #endif
  174. [BrowsableAttribute (false)]
  175. public
  176. #if NET_2_0
  177. override
  178. #endif
  179. ConnectionState State {
  180. get {
  181. if (gdaConnection != IntPtr.Zero) {
  182. if (libgda.gda_connection_is_open (gdaConnection))
  183. return ConnectionState.Open;
  184. }
  185. return ConnectionState.Closed;
  186. }
  187. }
  188. internal IntPtr GdaConnection {
  189. get {
  190. return gdaConnection;
  191. }
  192. }
  193. #endregion // Properties
  194. #region Methods
  195. public new OleDbTransaction BeginTransaction ()
  196. {
  197. if (gdaConnection != IntPtr.Zero)
  198. return new OleDbTransaction (this);
  199. return null;
  200. }
  201. public new OleDbTransaction BeginTransaction (IsolationLevel level)
  202. {
  203. if (gdaConnection != IntPtr.Zero)
  204. return new OleDbTransaction (this, level);
  205. return null;
  206. }
  207. #if NET_2_0
  208. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
  209. {
  210. return BeginTransaction (isolationLevel);
  211. }
  212. protected override DbCommand CreateDbCommand()
  213. {
  214. return CreateCommand ();
  215. }
  216. #else
  217. IDbTransaction IDbConnection.BeginTransaction ()
  218. {
  219. return BeginTransaction ();
  220. }
  221. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  222. {
  223. return BeginTransaction (level);
  224. }
  225. IDbCommand IDbConnection.CreateCommand ()
  226. {
  227. return CreateCommand ();
  228. }
  229. #endif
  230. public
  231. #if NET_2_0
  232. override
  233. #endif
  234. void ChangeDatabase (string name)
  235. {
  236. if (gdaConnection == IntPtr.Zero)
  237. throw new ArgumentException ();
  238. if (State != ConnectionState.Open)
  239. throw new InvalidOperationException ();
  240. if (!libgda.gda_connection_change_database (gdaConnection, name))
  241. throw new OleDbException (this);
  242. }
  243. public
  244. #if NET_2_0
  245. override
  246. #endif
  247. void Close ()
  248. {
  249. if (State == ConnectionState.Open) {
  250. libgda.gda_connection_close (gdaConnection);
  251. gdaConnection = IntPtr.Zero;
  252. }
  253. }
  254. public new OleDbCommand CreateCommand ()
  255. {
  256. if (State == ConnectionState.Open)
  257. return new OleDbCommand (null, this);
  258. return null;
  259. }
  260. [MonoTODO]
  261. protected override void Dispose (bool disposing)
  262. {
  263. throw new NotImplementedException ();
  264. }
  265. [MonoTODO]
  266. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  267. {
  268. throw new NotImplementedException ();
  269. }
  270. [MonoTODO]
  271. object ICloneable.Clone ()
  272. {
  273. throw new NotImplementedException();
  274. }
  275. public
  276. #if NET_2_0
  277. override
  278. #endif
  279. void Open ()
  280. {
  281. string provider = "Default";
  282. string gdaCncStr = "";
  283. string[] args;
  284. int len;
  285. char [] separator = { ';' };
  286. if (State == ConnectionState.Open)
  287. throw new InvalidOperationException ();
  288. gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
  289. connectionString, "", "", 0);
  290. if (gdaConnection==IntPtr.Zero)
  291. throw new OleDbException (this);
  292. /* convert the connection string to its GDA equivalent */
  293. //args = connectionString.Split (';');
  294. //len = args.Length;
  295. //for (int i = 0; i < len; i++) {
  296. // string[] values = args[i].Split (separator, 2);
  297. // if (values[0] == "Provider") {
  298. // if (values[1] == "SQLOLEDB")
  299. // provider = "FreeTDS";
  300. // else if (values[1] == "MSDAORA")
  301. // provider = "Oracle";
  302. // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
  303. // provider = "MS Access";
  304. // else
  305. // provider = values[2];
  306. // }
  307. // else if (values[0] == "Addr" || values[0] == "Address")
  308. // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
  309. // else if (values[0] == "Database")
  310. // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
  311. // else if (values[0] == "Connection Lifetime")
  312. // connectionTimeout = System.Convert.ToInt32 (values[1]);
  313. // else if (values[0] == "File Name")
  314. // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
  315. // else if (values[0] == "Password" || values[0] == "Pwd")
  316. // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
  317. // else if (values[0] == "User ID")
  318. // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
  319. //}
  320. /* open the connection */
  321. //System.Console.WriteLine ("Opening connection for provider " +
  322. // provider + " with " + gdaCncStr);
  323. //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
  324. // provider,
  325. // gdaCncStr);
  326. }
  327. [MonoTODO]
  328. public static void ReleaseObjectPool ()
  329. {
  330. throw new NotImplementedException ();
  331. }
  332. [MonoTODO]
  333. public void EnlistDistributedTransaction (ITransaction transaction)
  334. {
  335. throw new NotImplementedException ();
  336. }
  337. #if NET_2_0
  338. [MonoTODO]
  339. public override void EnlistTransaction (Transaction transaction)
  340. {
  341. throw new NotImplementedException ();
  342. }
  343. [MonoTODO]
  344. public override DataTable GetSchema ()
  345. {
  346. throw new NotImplementedException ();
  347. }
  348. [MonoTODO]
  349. public override DataTable GetSchema(string collectionName)
  350. {
  351. return GetSchema (collectionName, null);
  352. }
  353. [MonoTODO]
  354. public override DataTable GetSchema (String collectionName, string [] restrictionValues)
  355. {
  356. throw new NotImplementedException ();
  357. }
  358. [MonoTODO]
  359. [EditorBrowsable (EditorBrowsableState.Advanced)]
  360. public void ResetState()
  361. {
  362. throw new NotImplementedException ();
  363. }
  364. #endif
  365. #endregion
  366. #region Events and Delegates
  367. #if !NET_2_0
  368. [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
  369. #endif
  370. [DataCategory ("DataCategory_InfoMessage")]
  371. public event OleDbInfoMessageEventHandler InfoMessage;
  372. #if !NET_2_0
  373. [DataSysDescription ("Event triggered when the connection changes state.")]
  374. [DataCategory ("DataCategory_StateChange")]
  375. public event StateChangeEventHandler StateChange;
  376. #endif
  377. #endregion
  378. }
  379. }