2 /// DB2DriverCS - A DB2 driver for .Net
\r
3 /// Copyright 2003 By Christopher Bockner
\r
4 /// Released under the terms of the MIT/X11 Licence
\r
5 /// Please refer to the Licence.txt file that should be distributed with this package
\r
6 /// This software requires that DB2 client software be installed correctly on the machine
\r
7 /// (or instance) on which the driver is running.
\r
11 using System.Runtime.InteropServices;
\r
13 namespace DB2ClientCS
\r
15 /// <summary> /// This class is IDbConnection compliant. Refer to MSDN documentation for reference.
\r
18 public class DB2ClientConnection : IDbConnection
\r
20 private string connectionString = null;
\r
21 private string dbName = null;
\r
22 private int connectionTimeout;
\r
24 unsafe internal long dbHandle;
\r
25 private bool disposed = false;
\r
26 public DB2ClientConnection()
\r
29 // TODO: Add constructor logic here
\r
32 public DB2ClientConnection(string conString)
\r
34 this.connectionString = conString;
\r
36 #region ConnectionString property
\r
38 ///Accessor for the connectionString property
\r
39 public string ConnectionString
\r
43 return connectionString;
\r
47 connectionString = value;
\r
51 #region ConnectionTimeout property
\r
52 public int ConnectionTimeout
\r
56 return connectionTimeout;
\r
60 connectionTimeout = value;
\r
64 #region Database property
\r
65 public string Database
\r
77 #region State property
\r
79 /// The Connection State property, open or closed.
\r
80 /// NOTE: IBM's docs on SqlFreeHandle do not state what is done when a handle is freed
\r
81 /// i.e. if the handle is set to SQL_NULL_HANDLE.
\r
84 unsafe public ConnectionState State
\r
88 if (dbHandle == DB2ClientConstants.SQL_NULL_HANDLE)
\r
89 return ConnectionState.Closed;
\r
91 return ConnectionState.Open;
\r
97 /// Handle Returns an IntPtr of the dbm handle
\r
99 public IntPtr Handle
\r
103 return new IntPtr(dbHandle);
\r
108 #region BeginTransaction Method
\r
110 /// Opens a transaction against the database at the default isolation level, which will be
\r
111 /// that which the packages were bound at, unless overriden in the connection string, and if nothing was specified at that point
\r
112 /// then I believe the default level is Cursor Stability (don't quote me on that, I haven't
\r
113 /// found the appropriate reference yet), ODBC equivalent is SQL_TXN_READ_COMMITTED
\r
115 /// <returns></returns>
\r
116 public IDbTransaction BeginTransaction()
\r
121 #region BeginTransaction (IsolationLevel) Method
\r
123 /// BeginTransaction again overloadded to let us set the transaction level for the statement
\r
125 /// <param name="isolationL"></param>
\r
126 /// <returns></returns>
\r
127 public IDbTransaction BeginTransaction(IsolationLevel isolationL)
\r
132 #region ChangeDatabase
\r
133 unsafe public void ChangeDatabase(string newDBName)
\r
138 ///Close, per MSDN documentation
\r
140 unsafe public void Close()
\r
142 DB2ClientPrototypes.SQLDisconnect(dbHandle);
\r
143 dbHandle = DB2ClientConstants.SQL_NULL_HANDLE;
\r
146 #region CreateCommand
\r
148 /// CreateCommand per MSDN
\r
150 /// <returns></returns>
\r
151 public IDbCommand CreateCommand()
\r
154 return new DB2ClientCommand();
\r
161 unsafe public void Open()
\r
163 IntPtr pdbHandle = Marshal.AllocHGlobal(4);
\r
164 IntPtr penvHandle = Marshal.AllocHGlobal(4);
\r
168 IntPtr tempInt = IntPtr.Zero;
\r
169 sqlReturn = DB2ClientPrototypes.SQLAllocHandle(DB2ClientConstants.SQL_HANDLE_ENV, tempInt, ref penvHandle);
\r
170 sqlReturn = DB2ClientPrototypes.SQLAllocHandle(DB2ClientConstants.SQL_HANDLE_DBC, penvHandle, ref pdbHandle);
\r
172 if (sqlReturn == DB2ClientConstants.SQL_ERROR)
\r
174 throw new DB2ClientException(DB2ClientConstants.SQL_HANDLE_ENV, penvHandle, "Alloc Env Handle: ");
\r
176 sqlReturn = DB2ClientPrototypes.SQLConnect(pdbHandle, "sample", 6, "Administrator", 13, "tirpitz", 7);
\r
177 if (sqlReturn == DB2ClientConstants.SQL_ERROR)
\r
179 throw new DB2ClientException(DB2ClientConstants.SQL_HANDLE_DBC, pdbHandle, "Error connecting to DB: ");
\r
182 if(IntPtr.Size == 4)
\r
183 dbHandle = pdbHandle.ToInt32();
\r
185 dbHandle = pdbHandle.ToInt64();
\r
187 catch (DB2ClientException DB2E)
\r
189 Console.WriteLine(DB2E.Message);
\r
190 Marshal.FreeHGlobal(pdbHandle);
\r
191 Marshal.FreeHGlobal(penvHandle);
\r
194 Marshal.FreeHGlobal(pdbHandle);
\r
195 Marshal.FreeHGlobal(penvHandle);
\r
202 unsafe public void Dispose()
\r
215 /// Called after the connection string is set, this parses the it and saves the connection attributes.
\r
217 protected void ParseConnectionString()
\r
221 private void CheckState()
\r
223 if (ConnectionState.Closed == State)
\r
224 throw new DB2ClientException ("Connection is currently closed.");
\r