2 // System.Data.OleDb.OleDbConnection
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.ComponentModel;
37 using System.Data.Common;
38 using System.EnterpriseServices;
40 using System.Transactions;
43 namespace System.Data.OleDb
45 [DefaultEvent ("InfoMessage")]
47 public sealed class OleDbConnection : DbConnection, ICloneable
49 public sealed class OleDbConnection : Component, ICloneable, IDbConnection
54 string connectionString;
55 int connectionTimeout;
62 public OleDbConnection ()
64 gdaConnection = IntPtr.Zero;
65 connectionTimeout = 15;
68 public OleDbConnection (string connectionString) : this ()
70 this.connectionString = connectionString;
73 #endregion // Constructors
77 [DataCategory ("Data")]
79 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
80 [RecommendedAsConfigurable (true)]
81 [RefreshPropertiesAttribute (RefreshProperties.All)]
82 public override string ConnectionString {
84 if (connectionString == null)
86 return connectionString;
89 connectionString = value;
93 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
94 public override int ConnectionTimeout {
96 return connectionTimeout;
100 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
104 if (gdaConnection != IntPtr.Zero
105 && libgda.gda_connection_is_open (gdaConnection)) {
106 return libgda.gda_connection_get_database (gdaConnection);
113 [BrowsableAttribute (true)]
114 public override string DataSource {
116 if (gdaConnection != IntPtr.Zero
117 && libgda.gda_connection_is_open (gdaConnection)) {
118 return libgda.gda_connection_get_dsn (gdaConnection);
125 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
126 [BrowsableAttribute (true)]
127 public string Provider {
129 if (gdaConnection != IntPtr.Zero
130 && libgda.gda_connection_is_open (gdaConnection)) {
131 return libgda.gda_connection_get_provider (gdaConnection);
138 public override string ServerVersion {
140 if (State == ConnectionState.Closed)
141 throw ExceptionHelper.ConnectionClosed ();
142 return libgda.gda_connection_get_server_version (gdaConnection);
146 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
147 [BrowsableAttribute (false)]
148 public override ConnectionState State {
150 if (gdaConnection != IntPtr.Zero) {
151 if (libgda.gda_connection_is_open (gdaConnection))
152 return ConnectionState.Open;
155 return ConnectionState.Closed;
159 internal IntPtr GdaConnection {
161 return gdaConnection;
165 #endregion // Properties
169 public new OleDbTransaction BeginTransaction ()
171 if (State == ConnectionState.Closed)
172 throw ExceptionHelper.ConnectionClosed ();
173 return new OleDbTransaction (this);
176 public new OleDbTransaction BeginTransaction (IsolationLevel isolationLevel)
178 if (State == ConnectionState.Closed)
179 throw ExceptionHelper.ConnectionClosed ();
180 return new OleDbTransaction (this, isolationLevel);
183 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
185 return BeginTransaction (isolationLevel);
188 protected override DbCommand CreateDbCommand()
190 return CreateCommand ();
193 public override void ChangeDatabase (string value)
195 if (State != ConnectionState.Open)
196 throw new InvalidOperationException ();
198 if (!libgda.gda_connection_change_database (gdaConnection, value))
199 throw new OleDbException (this);
202 public override void Close ()
204 if (State == ConnectionState.Open) {
205 libgda.gda_connection_close (gdaConnection);
206 gdaConnection = IntPtr.Zero;
210 public new OleDbCommand CreateCommand ()
212 if (State == ConnectionState.Open)
213 return new OleDbCommand (null, this);
219 protected override void Dispose (bool disposing)
221 throw new NotImplementedException ();
225 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
227 throw new NotImplementedException ();
231 object ICloneable.Clone ()
233 throw new NotImplementedException();
242 // string provider = "Default";
243 // string gdaCncStr = string.Empty;
246 // char [] separator = { ';' };
248 if (State == ConnectionState.Open)
249 throw new InvalidOperationException ();
251 libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
253 gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
254 ConnectionString, string.Empty, string.Empty, 0);
256 if (gdaConnection == IntPtr.Zero)
257 throw new OleDbException (this);
258 /* convert the connection string to its GDA equivalent */
259 //args = connectionString.Split (';');
261 //for (int i = 0; i < len; i++) {
262 // string[] values = args[i].Split (separator, 2);
263 // if (values[0] == "Provider") {
264 // if (values[1] == "SQLOLEDB")
265 // provider = "FreeTDS";
266 // else if (values[1] == "MSDAORA")
267 // provider = "Oracle";
268 // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
269 // provider = "MS Access";
271 // provider = values[2];
273 // else if (values[0] == "Addr" || values[0] == "Address")
274 // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
275 // else if (values[0] == "Database")
276 // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
277 // else if (values[0] == "Connection Lifetime")
278 // connectionTimeout = System.Convert.ToInt32 (values[1]);
279 // else if (values[0] == "File Name")
280 // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
281 // else if (values[0] == "Password" || values[0] == "Pwd")
282 // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
283 // else if (values[0] == "User ID")
284 // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
287 /* open the connection */
288 //System.Console.WriteLine ("Opening connection for provider " +
289 // provider + " with " + gdaCncStr);
290 //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
296 public static void ReleaseObjectPool ()
298 throw new NotImplementedException ();
302 public void EnlistDistributedTransaction (ITransaction transaction)
304 throw new NotImplementedException ();
309 public override void EnlistTransaction (Transaction transaction)
311 throw new NotImplementedException ();
315 public override DataTable GetSchema ()
317 if (State == ConnectionState.Closed)
318 throw ExceptionHelper.ConnectionClosed ();
319 throw new NotImplementedException ();
323 public override DataTable GetSchema(string collectionName)
325 return GetSchema (collectionName, null);
329 public override DataTable GetSchema (String collectionName, string [] restrictionValues)
331 if (State == ConnectionState.Closed)
332 throw ExceptionHelper.ConnectionClosed ();
333 throw new NotImplementedException ();
337 [EditorBrowsable (EditorBrowsableState.Advanced)]
338 public void ResetState ()
340 throw new NotImplementedException ();
346 #region Events and Delegates
349 [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
351 [DataCategory ("DataCategory_InfoMessage")]
352 public event OleDbInfoMessageEventHandler InfoMessage;
355 [DataSysDescription ("Event triggered when the connection changes state.")]
356 [DataCategory ("DataCategory_StateChange")]
357 public event StateChangeEventHandler StateChange;