OleDbConnection.cs 11 KB

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