OdbcConnection.cs 5.2 KB

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