OdbcConnection.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // System.Data.Odbc.OdbcConnection
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Data.Common;
  12. namespace System.Data.Odbc
  13. {
  14. public sealed class OdbcConnection : Component, ICloneable, IDbConnection
  15. {
  16. #region Fields
  17. string connectionString;
  18. int connectionTimeout;
  19. OdbcDataReader dataReader;
  20. internal OdbcTransaction transaction;
  21. IntPtr henv=IntPtr.Zero, hdbc=IntPtr.Zero;
  22. #endregion
  23. #region Constructors
  24. public OdbcConnection ()
  25. {
  26. OdbcReturn ret;
  27. // allocate Environment handle
  28. ret=libodbc.SQLAllocHandle(OdbcHandleType.Env, IntPtr.Zero, ref henv);
  29. libodbchelper.DisplayError("SQLAllocHandle", ret);
  30. ret=libodbc.SQLSetEnvAttr(henv, OdbcEnv.OdbcVersion, (IntPtr) 3 , 0);
  31. libodbchelper.DisplayError("SQLSetEnvAttr", ret);
  32. Console.WriteLine("ODBCInit Complete.");
  33. connectionTimeout = 15;
  34. connectionString = null;
  35. dataReader = null;
  36. }
  37. public OdbcConnection (string connectionString) : this ()
  38. {
  39. ConnectionString = connectionString;
  40. }
  41. #endregion // Constructors
  42. #region Properties
  43. internal IntPtr hDbc
  44. {
  45. get { return hdbc; }
  46. }
  47. public string ConnectionString {
  48. get {
  49. return connectionString;
  50. }
  51. set {
  52. connectionString = value;
  53. }
  54. }
  55. public int ConnectionTimeout {
  56. get {
  57. return connectionTimeout;
  58. }
  59. }
  60. // public string DataSource {
  61. // get {
  62. // if (State==ConnectionState.Open)
  63. // return _dsn;
  64. // else
  65. // return null;
  66. // }
  67. // }
  68. public string Database {
  69. get {
  70. return "";
  71. }
  72. }
  73. public ConnectionState State
  74. {
  75. get {
  76. if (hdbc!=IntPtr.Zero) {
  77. return ConnectionState.Open;
  78. }
  79. else
  80. return ConnectionState.Closed;
  81. }
  82. }
  83. internal OdbcDataReader DataReader
  84. {
  85. get {
  86. return dataReader;
  87. }
  88. set {
  89. dataReader = value;
  90. }
  91. }
  92. #endregion // Properties
  93. #region Methods
  94. public OdbcTransaction BeginTransaction ()
  95. {
  96. return BeginTransaction(IsolationLevel.Unspecified);
  97. }
  98. IDbTransaction IDbConnection.BeginTransaction ()
  99. {
  100. return (IDbTransaction) BeginTransaction();
  101. }
  102. public OdbcTransaction BeginTransaction (IsolationLevel level)
  103. {
  104. if (transaction==null)
  105. {
  106. transaction=new OdbcTransaction(this,level);
  107. return transaction;
  108. }
  109. else
  110. throw new InvalidOperationException();
  111. }
  112. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  113. {
  114. return (IDbTransaction) BeginTransaction(level);
  115. }
  116. public void Close ()
  117. {
  118. if (State == ConnectionState.Open) {
  119. // TODO: Free handles
  120. dataReader = null;
  121. hdbc = IntPtr.Zero;
  122. transaction=null;
  123. }
  124. else
  125. throw new InvalidOperationException();
  126. }
  127. public OdbcCommand CreateCommand ()
  128. {
  129. return new OdbcCommand("", this, transaction);
  130. }
  131. [MonoTODO]
  132. public void ChangeDatabase(string Database)
  133. {
  134. throw new NotImplementedException ();
  135. }
  136. [MonoTODO]
  137. protected override void Dispose (bool disposing)
  138. {
  139. }
  140. [MonoTODO]
  141. object ICloneable.Clone ()
  142. {
  143. throw new NotImplementedException();
  144. }
  145. IDbCommand IDbConnection.CreateCommand ()
  146. {
  147. return (IDbCommand) CreateCommand ();
  148. }
  149. public void Open ()
  150. {
  151. if (State == ConnectionState.Open)
  152. throw new InvalidOperationException ();
  153. // allocate connection handle
  154. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Dbc, henv, ref hdbc);
  155. libodbchelper.DisplayError("SQLAllocHandle(hdbc)", ret);
  156. // DSN connection
  157. if (connectionString.ToLower().IndexOf("dsn=")>=0)
  158. {
  159. string _uid="", _pwd="", _dsn="";
  160. string[] items=connectionString.Split(new char[1]{';'});
  161. foreach (string item in items)
  162. {
  163. string[] parts=item.Split(new char[1] {'='});
  164. switch (parts[0].Trim().ToLower())
  165. {
  166. case "dsn":
  167. _dsn=parts[1].Trim();
  168. break;
  169. case "uid":
  170. _uid=parts[1].Trim();
  171. break;
  172. case "pwd":
  173. _pwd=parts[1].Trim();
  174. break;
  175. }
  176. }
  177. ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
  178. libodbchelper.DisplayError("SQLConnect",ret);
  179. }
  180. else
  181. {
  182. // DSN-less Connection
  183. string OutConnectionString=new String(' ',1024);
  184. short OutLen=0;
  185. ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, connectionString, -3,
  186. OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
  187. libodbchelper.DisplayError("SQLConnect",ret);
  188. }
  189. }
  190. [MonoTODO]
  191. public static void ReleaseObjectPool ()
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. #endregion
  196. #region Events and Delegates
  197. public event StateChangeEventHandler StateChange;
  198. #endregion
  199. }
  200. }