OleDbConnection.cs 9.4 KB

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