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
12 using System.ComponentModel;
14 using System.Data.Common;
16 namespace System.Data.OleDb
18 public sealed class OleDbConnection : Component, ICloneable, IDbConnection
22 string connectionString;
23 int connectionTimeout;
24 OleDbDataReader dataReader;
31 public OleDbConnection ()
33 libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
34 gdaConnection = IntPtr.Zero;
35 connectionTimeout = 15;
36 connectionString = null;
40 public OleDbConnection (string connectionString) : this ()
42 this.connectionString = connectionString;
45 #endregion // Constructors
49 public string ConnectionString {
51 return connectionString;
54 connectionString = value;
58 public int ConnectionTimeout {
60 return connectionTimeout;
64 public string Database {
66 if (gdaConnection != IntPtr.Zero
67 && libgda.gda_connection_is_open (gdaConnection)) {
68 return libgda.gda_connection_get_database (gdaConnection);
75 public string DataSource {
77 if (gdaConnection != IntPtr.Zero
78 && libgda.gda_connection_is_open (gdaConnection)) {
79 return libgda.gda_connection_get_dsn (gdaConnection);
86 public string Provider {
88 if (gdaConnection != IntPtr.Zero
89 && libgda.gda_connection_is_open (gdaConnection)) {
90 return libgda.gda_connection_get_provider (gdaConnection);
97 public string ServerVersion {
99 if (gdaConnection != IntPtr.Zero
100 && libgda.gda_connection_is_open (gdaConnection)) {
101 return libgda.gda_connection_get_server_version (gdaConnection);
108 public ConnectionState State
111 if (gdaConnection != IntPtr.Zero) {
112 if (libgda.gda_connection_is_open (gdaConnection))
113 return ConnectionState.Open;
116 return ConnectionState.Closed;
120 internal IntPtr GdaConnection
123 return gdaConnection;
127 internal OleDbDataReader DataReader
137 #endregion // Properties
141 public OleDbTransaction BeginTransaction ()
143 if (gdaConnection != IntPtr.Zero)
144 return new OleDbTransaction (this);
149 IDbTransaction IDbConnection.BeginTransaction ()
151 return BeginTransaction ();
154 public OleDbTransaction BeginTransaction (IsolationLevel level)
156 if (gdaConnection != IntPtr.Zero)
157 return new OleDbTransaction (this, level);
162 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
164 return BeginTransaction (level);
167 public void ChangeDatabase (string name)
169 if (gdaConnection == IntPtr.Zero)
170 throw new ArgumentException ();
171 if (State != ConnectionState.Open)
172 throw new InvalidOperationException ();
174 if (!libgda.gda_connection_change_database (gdaConnection, name))
175 throw new OleDbException (this);
180 if (State == ConnectionState.Open) {
181 libgda.gda_connection_close (gdaConnection);
182 gdaConnection = IntPtr.Zero;
188 public OleDbCommand CreateCommand ()
190 if (State == ConnectionState.Open &&
192 return new OleDbCommand (null, this);
198 protected override void Dispose (bool disposing)
200 throw new NotImplementedException ();
204 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
206 throw new NotImplementedException ();
210 object ICloneable.Clone ()
212 throw new NotImplementedException();
215 IDbCommand IDbConnection.CreateCommand ()
217 return CreateCommand ();
222 string provider = "Default";
223 string gdaCncStr = "";
226 char [] separator = { ';' };
228 if (State == ConnectionState.Open)
229 throw new InvalidOperationException ();
231 /* convert the connection string to its GDA equivalent */
232 args = connectionString.Split (';');
234 for (int i = 0; i < len; i++) {
235 string[] values = args[i].Split (separator, 2);
236 if (values[0] == "Provider") {
237 if (values[1] == "SQLOLEDB")
238 provider = "FreeTDS";
239 else if (values[1] == "MSDAORA")
241 else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
242 provider = "MS Access";
244 provider = values[2];
246 else if (values[0] == "Addr" || values[0] == "Address")
247 gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
248 else if (values[0] == "Database")
249 gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
250 else if (values[0] == "Connection Lifetime")
251 connectionTimeout = System.Convert.ToInt32 (values[1]);
252 else if (values[0] == "File Name")
253 gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
254 else if (values[0] == "Password" || values[0] == "Pwd")
255 gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
256 else if (values[0] == "User ID")
257 gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
260 /* open the connection */
261 System.Console.WriteLine ("Opening connection for provider " +
262 provider + " with " + gdaCncStr);
263 gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
269 public static void ReleaseObjectPool ()
271 throw new NotImplementedException ();
276 #region Events and Delegates
278 public event OleDbInfoMessageEventHandler InfoMessage;
279 public event StateChangeEventHandler StateChange;