2 // System.Data.Odbc.OdbcConnection
5 // Brian Ritchie (brianlritchie@hotmail.com)
7 // Copyright (C) Brian Ritchie, 2002
10 using System.ComponentModel;
12 using System.Data.Common;
14 namespace System.Data.Odbc
16 [DefaultEvent("InfoMessage")]
17 public sealed class OdbcConnection : Component, ICloneable, IDbConnection
21 string connectionString;
22 int connectionTimeout;
23 internal OdbcTransaction transaction;
24 IntPtr henv=IntPtr.Zero, hdbc=IntPtr.Zero;
30 public OdbcConnection ()
34 // allocate Environment handle
35 ret=libodbc.SQLAllocHandle(OdbcHandleType.Env, IntPtr.Zero, ref henv);
36 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
37 throw new OdbcException(new OdbcError("SQLAllocHandle"));
39 ret=libodbc.SQLSetEnvAttr(henv, OdbcEnv.OdbcVersion, (IntPtr) 3 , 0);
40 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
41 throw new OdbcException(new OdbcError("SQLSetEnvAttr",OdbcHandleType.Env,henv));
43 connectionTimeout = 15;
44 connectionString = null;
47 public OdbcConnection (string connectionString) : this ()
49 ConnectionString = connectionString;
52 #endregion // Constructors
61 public string ConnectionString {
63 return connectionString;
66 connectionString = value;
70 public int ConnectionTimeout {
72 return connectionTimeout;
76 throw new ArgumentException("Timout should not be less than zero.");
\r
78 connectionTimeout = value;
\r
82 // public string DataSource {
84 // if (State==ConnectionState.Open)
91 public string Database {
97 public ConnectionState State
100 if (hdbc!=IntPtr.Zero) {
101 return ConnectionState.Open;
104 return ConnectionState.Closed;
108 #endregion // Properties
112 public OdbcTransaction BeginTransaction ()
114 return BeginTransaction(IsolationLevel.Unspecified);
117 IDbTransaction IDbConnection.BeginTransaction ()
119 return (IDbTransaction) BeginTransaction();
122 public OdbcTransaction BeginTransaction (IsolationLevel level)
124 if (transaction==null)
126 transaction=new OdbcTransaction(this,level);
130 throw new InvalidOperationException();
133 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
135 return (IDbTransaction) BeginTransaction(level);
140 if (State == ConnectionState.Open) {
141 // TODO: Free handles
146 throw new InvalidOperationException();
149 public OdbcCommand CreateCommand ()
151 return new OdbcCommand("", this, transaction);
155 public void ChangeDatabase(string Database)
157 throw new NotImplementedException ();
161 protected override void Dispose (bool disposing)
166 object ICloneable.Clone ()
168 throw new NotImplementedException();
171 IDbCommand IDbConnection.CreateCommand ()
173 return (IDbCommand) CreateCommand ();
178 if (State == ConnectionState.Open)
179 throw new InvalidOperationException ();
181 // allocate connection handle
182 OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Dbc, henv, ref hdbc);
183 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
184 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Env,henv));
187 if (connectionString.ToLower().IndexOf("dsn=")>=0)
189 string _uid="", _pwd="", _dsn="";
190 string[] items=connectionString.Split(new char[1]{';'});
191 foreach (string item in items)
193 string[] parts=item.Split(new char[1] {'='});
194 switch (parts[0].Trim().ToLower())
197 _dsn=parts[1].Trim();
200 _uid=parts[1].Trim();
203 _pwd=parts[1].Trim();
207 ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
208 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
209 throw new OdbcException(new OdbcError("SQLConnect",OdbcHandleType.Dbc,hdbc));
213 // DSN-less Connection
214 string OutConnectionString=new String(' ',1024);
216 ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, connectionString, -3,
217 OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
218 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
219 throw new OdbcException(new OdbcError("SQLDriverConnect",OdbcHandleType.Dbc,hdbc));
225 public static void ReleaseObjectPool ()
227 throw new NotImplementedException ();
232 #region Events and Delegates
234 public event StateChangeEventHandler StateChange;