OdbcConnection.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. using System.EnterpriseServices;
  13. namespace System.Data.Odbc
  14. {
  15. [DefaultEvent("InfoMessage")]
  16. public sealed class OdbcConnection : Component, ICloneable, IDbConnection
  17. {
  18. #region Fields
  19. string connectionString;
  20. int connectionTimeout;
  21. internal OdbcTransaction transaction;
  22. IntPtr henv=IntPtr.Zero, hdbc=IntPtr.Zero;
  23. #endregion
  24. #region Constructors
  25. public OdbcConnection ()
  26. {
  27. OdbcReturn ret;
  28. // allocate Environment handle
  29. ret=libodbc.SQLAllocHandle(OdbcHandleType.Env, IntPtr.Zero, ref henv);
  30. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  31. throw new OdbcException(new OdbcError("SQLAllocHandle"));
  32. ret=libodbc.SQLSetEnvAttr(henv, OdbcEnv.OdbcVersion, (IntPtr) 3 , 0);
  33. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  34. throw new OdbcException(new OdbcError("SQLSetEnvAttr",OdbcHandleType.Env,henv));
  35. connectionTimeout = 15;
  36. connectionString = null;
  37. }
  38. public OdbcConnection (string connectionString) : this ()
  39. {
  40. ConnectionString = connectionString;
  41. }
  42. #endregion // Constructors
  43. #region Properties
  44. internal IntPtr hDbc
  45. {
  46. get { return hdbc; }
  47. }
  48. [OdbcCategoryAttribute ("DataCategory_Data")]
  49. [DefaultValue ("")]
  50. [OdbcDescriptionAttribute ("Information used to connect to a Data Source")]
  51. [RefreshPropertiesAttribute (RefreshProperties.All)]
  52. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  53. [RecommendedAsConfigurableAttribute (true)]
  54. public string ConnectionString {
  55. get {
  56. return connectionString;
  57. }
  58. set {
  59. connectionString = value;
  60. }
  61. }
  62. [OdbcDescriptionAttribute ("Current connection timeout value, not settable in the ConnectionString")]
  63. [DefaultValue (15)]
  64. public int ConnectionTimeout {
  65. get {
  66. return connectionTimeout;
  67. }
  68. set {
  69. if (value < 0) {
  70. throw new ArgumentException("Timout should not be less than zero.");
  71. }
  72. connectionTimeout = value;
  73. }
  74. }
  75. // public string DataSource {
  76. // get {
  77. // if (State==ConnectionState.Open)
  78. // return _dsn;
  79. // else
  80. // return null;
  81. // }
  82. // }
  83. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  84. [OdbcDescriptionAttribute ("Current data source Catlog value, 'Database=X' in the ConnectionString")]
  85. public string Database {
  86. get {
  87. return "";
  88. }
  89. }
  90. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  91. [OdbcDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed")]
  92. [BrowsableAttribute (false)]
  93. public ConnectionState State
  94. {
  95. get {
  96. if (hdbc!=IntPtr.Zero) {
  97. return ConnectionState.Open;
  98. }
  99. else
  100. return ConnectionState.Closed;
  101. }
  102. }
  103. [MonoTODO]
  104. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  105. [OdbcDescriptionAttribute ("Current data source, 'Server=X' in the ConnectionString")]
  106. public string DataSource {
  107. get {
  108. throw new NotImplementedException ();
  109. }
  110. }
  111. [MonoTODO]
  112. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  113. [OdbcDescriptionAttribute ("Current ODBC Driver")]
  114. public string Driver {
  115. get {
  116. throw new NotImplementedException ();
  117. }
  118. }
  119. [MonoTODO]
  120. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  121. [OdbcDescriptionAttribute ("Version of the product accessed by the ODBC Driver")]
  122. [BrowsableAttribute (false)]
  123. public string ServerVersion {
  124. get {
  125. throw new NotImplementedException ();
  126. }
  127. }
  128. #endregion // Properties
  129. #region Methods
  130. public OdbcTransaction BeginTransaction ()
  131. {
  132. return BeginTransaction(IsolationLevel.Unspecified);
  133. }
  134. IDbTransaction IDbConnection.BeginTransaction ()
  135. {
  136. return (IDbTransaction) BeginTransaction();
  137. }
  138. public OdbcTransaction BeginTransaction (IsolationLevel level)
  139. {
  140. if (transaction==null)
  141. {
  142. transaction=new OdbcTransaction(this,level);
  143. return transaction;
  144. }
  145. else
  146. throw new InvalidOperationException();
  147. }
  148. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  149. {
  150. return (IDbTransaction) BeginTransaction(level);
  151. }
  152. public void Close ()
  153. {
  154. if (State == ConnectionState.Open) {
  155. // TODO: Free handles
  156. hdbc = IntPtr.Zero;
  157. transaction=null;
  158. }
  159. else
  160. throw new InvalidOperationException();
  161. }
  162. public OdbcCommand CreateCommand ()
  163. {
  164. return new OdbcCommand("", this, transaction);
  165. }
  166. [MonoTODO]
  167. public void ChangeDatabase(string Database)
  168. {
  169. throw new NotImplementedException ();
  170. }
  171. [MonoTODO]
  172. protected override void Dispose (bool disposing)
  173. {
  174. }
  175. [MonoTODO]
  176. object ICloneable.Clone ()
  177. {
  178. throw new NotImplementedException();
  179. }
  180. IDbCommand IDbConnection.CreateCommand ()
  181. {
  182. return (IDbCommand) CreateCommand ();
  183. }
  184. public void Open ()
  185. {
  186. if (State == ConnectionState.Open)
  187. throw new InvalidOperationException ();
  188. // allocate connection handle
  189. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Dbc, henv, ref hdbc);
  190. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  191. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Env,henv));
  192. // DSN connection
  193. if (connectionString.ToLower().IndexOf("dsn=")>=0)
  194. {
  195. string _uid="", _pwd="", _dsn="";
  196. string[] items=connectionString.Split(new char[1]{';'});
  197. foreach (string item in items)
  198. {
  199. string[] parts=item.Split(new char[1] {'='});
  200. switch (parts[0].Trim().ToLower())
  201. {
  202. case "dsn":
  203. _dsn=parts[1].Trim();
  204. break;
  205. case "uid":
  206. _uid=parts[1].Trim();
  207. break;
  208. case "pwd":
  209. _pwd=parts[1].Trim();
  210. break;
  211. }
  212. }
  213. ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
  214. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  215. throw new OdbcException(new OdbcError("SQLConnect",OdbcHandleType.Dbc,hdbc));
  216. }
  217. else
  218. {
  219. // DSN-less Connection
  220. string OutConnectionString=new String(' ',1024);
  221. short OutLen=0;
  222. ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, connectionString, -3,
  223. OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
  224. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  225. throw new OdbcException(new OdbcError("SQLDriverConnect",OdbcHandleType.Dbc,hdbc));
  226. }
  227. }
  228. [MonoTODO]
  229. public static void ReleaseObjectPool ()
  230. {
  231. throw new NotImplementedException ();
  232. }
  233. [MonoTODO]
  234. public void EnlistDistributedTransaction ( ITransaction transaction) {
  235. throw new NotImplementedException ();
  236. }
  237. #endregion
  238. #region Events and Delegates
  239. [OdbcDescription ("DbConnection_StateChange")]
  240. [OdbcCategory ("DataCategory_StateChange")]
  241. public event StateChangeEventHandler StateChange;
  242. [OdbcDescription ("DbConnection_InfoMessage")]
  243. [OdbcCategory ("DataCategory_InfoMessage")]
  244. public event OdbcInfoMessageEventHandler InfoMessage;
  245. #endregion
  246. }
  247. }