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 public sealed class OdbcConnection : Component, ICloneable, IDbConnection
20 string connectionString;
21 int connectionTimeout;
22 OdbcDataReader dataReader;
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 libodbchelper.DisplayError("SQLAllocHandle", ret);
38 ret=libodbc.SQLSetEnvAttr(henv, OdbcEnv.OdbcVersion, (IntPtr) 3 , 0);
39 libodbchelper.DisplayError("SQLSetEnvAttr", ret);
41 //Console.WriteLine("ODBCInit Complete.");
42 connectionTimeout = 15;
43 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 // public string DataSource {
78 // if (State==ConnectionState.Open)
85 public string Database {
91 public ConnectionState State
94 if (hdbc!=IntPtr.Zero) {
95 return ConnectionState.Open;
98 return ConnectionState.Closed;
102 internal OdbcDataReader DataReader
112 #endregion // Properties
116 public OdbcTransaction BeginTransaction ()
118 return BeginTransaction(IsolationLevel.Unspecified);
121 IDbTransaction IDbConnection.BeginTransaction ()
123 return (IDbTransaction) BeginTransaction();
126 public OdbcTransaction BeginTransaction (IsolationLevel level)
128 if (transaction==null)
130 transaction=new OdbcTransaction(this,level);
134 throw new InvalidOperationException();
137 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
139 return (IDbTransaction) BeginTransaction(level);
144 if (State == ConnectionState.Open) {
145 // TODO: Free handles
151 throw new InvalidOperationException();
154 public OdbcCommand CreateCommand ()
156 return new OdbcCommand("", this, transaction);
160 public void ChangeDatabase(string Database)
162 throw new NotImplementedException ();
166 protected override void Dispose (bool disposing)
171 object ICloneable.Clone ()
173 throw new NotImplementedException();
176 IDbCommand IDbConnection.CreateCommand ()
178 return (IDbCommand) CreateCommand ();
183 if (State == ConnectionState.Open)
184 throw new InvalidOperationException ();
186 // allocate connection handle
187 OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Dbc, henv, ref hdbc);
188 libodbchelper.DisplayError("SQLAllocHandle(hdbc)", ret);
191 if (connectionString.ToLower().IndexOf("dsn=")>=0)
193 string _uid="", _pwd="", _dsn="";
194 string[] items=connectionString.Split(new char[1]{';'});
195 foreach (string item in items)
197 string[] parts=item.Split(new char[1] {'='});
198 switch (parts[0].Trim().ToLower())
201 _dsn=parts[1].Trim();
204 _uid=parts[1].Trim();
207 _pwd=parts[1].Trim();
211 ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
212 libodbchelper.DisplayError("SQLConnect",ret);
216 // DSN-less Connection
217 string OutConnectionString=new String(' ',1024);
219 ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, connectionString, -3,
220 OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
221 libodbchelper.DisplayError("SQLConnect",ret);
227 public static void ReleaseObjectPool ()
229 throw new NotImplementedException ();
234 #region Events and Delegates
236 public event StateChangeEventHandler StateChange;