OdbcConnection.cs 5.1 KB

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