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;
39 using System.Transactions;
41 namespace System.Data.OleDb
43 [DefaultEvent ("InfoMessage")]
44 public sealed class OleDbConnection : DbConnection, ICloneable
48 string connectionString;
49 int connectionTimeout;
56 public OleDbConnection ()
58 gdaConnection = IntPtr.Zero;
59 connectionTimeout = 15;
62 public OleDbConnection (string connectionString) : this ()
64 this.connectionString = connectionString;
67 #endregion // Constructors
71 [DataCategory ("Data")]
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 override string ConnectionString {
78 if (connectionString == null)
80 return connectionString;
83 connectionString = value;
87 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
88 public override int ConnectionTimeout {
90 return connectionTimeout;
94 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
98 if (gdaConnection != IntPtr.Zero
99 && libgda.gda_connection_is_open (gdaConnection)) {
100 return libgda.gda_connection_get_database (gdaConnection);
107 [BrowsableAttribute (true)]
108 public override string DataSource {
110 if (gdaConnection != IntPtr.Zero
111 && libgda.gda_connection_is_open (gdaConnection)) {
112 return libgda.gda_connection_get_dsn (gdaConnection);
119 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
120 [BrowsableAttribute (true)]
121 public string Provider {
123 if (gdaConnection != IntPtr.Zero
124 && libgda.gda_connection_is_open (gdaConnection)) {
125 return libgda.gda_connection_get_provider (gdaConnection);
132 public override string ServerVersion {
134 if (State == ConnectionState.Closed)
135 throw ExceptionHelper.ConnectionClosed ();
136 return libgda.gda_connection_get_server_version (gdaConnection);
140 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
141 [BrowsableAttribute (false)]
142 public override ConnectionState State {
144 if (gdaConnection != IntPtr.Zero) {
145 if (libgda.gda_connection_is_open (gdaConnection))
146 return ConnectionState.Open;
149 return ConnectionState.Closed;
153 internal IntPtr GdaConnection {
155 return gdaConnection;
159 #endregion // Properties
163 public new OleDbTransaction BeginTransaction ()
165 if (State == ConnectionState.Closed)
166 throw ExceptionHelper.ConnectionClosed ();
167 return new OleDbTransaction (this);
170 public new OleDbTransaction BeginTransaction (IsolationLevel isolationLevel)
172 if (State == ConnectionState.Closed)
173 throw ExceptionHelper.ConnectionClosed ();
174 return new OleDbTransaction (this, isolationLevel);
177 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
179 return BeginTransaction (isolationLevel);
182 protected override DbCommand CreateDbCommand()
184 return CreateCommand ();
187 public override void ChangeDatabase (string value)
189 if (State != ConnectionState.Open)
190 throw new InvalidOperationException ();
192 if (!libgda.gda_connection_change_database (gdaConnection, value))
193 throw new OleDbException (this);
196 public override void Close ()
198 if (State == ConnectionState.Open) {
199 libgda.gda_connection_close (gdaConnection);
200 gdaConnection = IntPtr.Zero;
204 public new OleDbCommand CreateCommand ()
206 if (State == ConnectionState.Open)
207 return new OleDbCommand (null, this);
213 protected override void Dispose (bool disposing)
215 throw new NotImplementedException ();
219 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
221 throw new NotImplementedException ();
225 object ICloneable.Clone ()
227 throw new NotImplementedException();
234 // string provider = "Default";
235 // string gdaCncStr = string.Empty;
238 // char [] separator = { ';' };
240 if (State == ConnectionState.Open)
241 throw new InvalidOperationException ();
243 libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
245 gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
246 ConnectionString, string.Empty, string.Empty, 0);
248 if (gdaConnection == IntPtr.Zero)
249 throw new OleDbException (this);
250 /* convert the connection string to its GDA equivalent */
251 //args = connectionString.Split (';');
253 //for (int i = 0; i < len; i++) {
254 // string[] values = args[i].Split (separator, 2);
255 // if (values[0] == "Provider") {
256 // if (values[1] == "SQLOLEDB")
257 // provider = "FreeTDS";
258 // else if (values[1] == "MSDAORA")
259 // provider = "Oracle";
260 // else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
261 // provider = "MS Access";
263 // provider = values[2];
265 // else if (values[0] == "Addr" || values[0] == "Address")
266 // gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
267 // else if (values[0] == "Database")
268 // gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
269 // else if (values[0] == "Connection Lifetime")
270 // connectionTimeout = System.Convert.ToInt32 (values[1]);
271 // else if (values[0] == "File Name")
272 // gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
273 // else if (values[0] == "Password" || values[0] == "Pwd")
274 // gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
275 // else if (values[0] == "User ID")
276 // gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
279 /* open the connection */
280 //System.Console.WriteLine ("Opening connection for provider " +
281 // provider + " with " + gdaCncStr);
282 //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
288 public static void ReleaseObjectPool ()
290 throw new NotImplementedException ();
294 public void EnlistDistributedTransaction (ITransaction transaction)
296 throw new NotImplementedException ();
300 public override void EnlistTransaction (Transaction transaction)
302 throw new NotImplementedException ();
306 public override DataTable GetSchema ()
308 if (State == ConnectionState.Closed)
309 throw ExceptionHelper.ConnectionClosed ();
310 throw new NotImplementedException ();
314 public override DataTable GetSchema(string collectionName)
316 return GetSchema (collectionName, null);
320 public override DataTable GetSchema (String collectionName, string [] restrictionValues)
322 if (State == ConnectionState.Closed)
323 throw ExceptionHelper.ConnectionClosed ();
324 throw new NotImplementedException ();
328 [EditorBrowsable (EditorBrowsableState.Advanced)]
329 public void ResetState ()
331 throw new NotImplementedException ();
336 #region Events and Delegates
338 [DataCategory ("DataCategory_InfoMessage")]
339 public event OleDbInfoMessageEventHandler InfoMessage;