OdbcConnection.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // System.Data.Odbc.OdbcConnection
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.ComponentModel;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.EnterpriseServices;
  35. namespace System.Data.Odbc
  36. {
  37. [DefaultEvent("InfoMessage")]
  38. public sealed class OdbcConnection : Component, ICloneable, IDbConnection
  39. {
  40. #region Fields
  41. string connectionString;
  42. int connectionTimeout;
  43. internal OdbcTransaction transaction;
  44. IntPtr henv=IntPtr.Zero, hdbc=IntPtr.Zero;
  45. #endregion
  46. #region Constructors
  47. public OdbcConnection ()
  48. {
  49. OdbcReturn ret;
  50. // allocate Environment handle
  51. ret=libodbc.SQLAllocHandle(OdbcHandleType.Env, IntPtr.Zero, ref henv);
  52. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  53. throw new OdbcException(new OdbcError("SQLAllocHandle"));
  54. ret=libodbc.SQLSetEnvAttr(henv, OdbcEnv.OdbcVersion, (IntPtr) 3 , 0);
  55. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  56. throw new OdbcException(new OdbcError("SQLSetEnvAttr",OdbcHandleType.Env,henv));
  57. connectionTimeout = 15;
  58. connectionString = null;
  59. }
  60. public OdbcConnection (string connectionString) : this ()
  61. {
  62. ConnectionString = connectionString;
  63. }
  64. #endregion // Constructors
  65. #region Properties
  66. internal IntPtr hDbc
  67. {
  68. get { return hdbc; }
  69. }
  70. [OdbcCategoryAttribute ("DataCategory_Data")]
  71. [DefaultValue ("")]
  72. [OdbcDescriptionAttribute ("Information used to connect to a Data Source")]
  73. [RefreshPropertiesAttribute (RefreshProperties.All)]
  74. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  75. [RecommendedAsConfigurableAttribute (true)]
  76. public string ConnectionString {
  77. get {
  78. return connectionString;
  79. }
  80. set {
  81. connectionString = value;
  82. }
  83. }
  84. [OdbcDescriptionAttribute ("Current connection timeout value, not settable in the ConnectionString")]
  85. [DefaultValue (15)]
  86. public int ConnectionTimeout {
  87. get {
  88. return connectionTimeout;
  89. }
  90. set {
  91. if (value < 0) {
  92. throw new ArgumentException("Timout should not be less than zero.");
  93. }
  94. connectionTimeout = value;
  95. }
  96. }
  97. // public string DataSource {
  98. // get {
  99. // if (State==ConnectionState.Open)
  100. // return _dsn;
  101. // else
  102. // return null;
  103. // }
  104. // }
  105. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  106. [OdbcDescriptionAttribute ("Current data source Catlog value, 'Database=X' in the ConnectionString")]
  107. public string Database {
  108. get {
  109. return "";
  110. }
  111. }
  112. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  113. [OdbcDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed")]
  114. [BrowsableAttribute (false)]
  115. public ConnectionState State
  116. {
  117. get {
  118. if (hdbc!=IntPtr.Zero) {
  119. return ConnectionState.Open;
  120. }
  121. else
  122. return ConnectionState.Closed;
  123. }
  124. }
  125. [MonoTODO]
  126. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  127. [OdbcDescriptionAttribute ("Current data source, 'Server=X' in the ConnectionString")]
  128. public string DataSource {
  129. get {
  130. throw new NotImplementedException ();
  131. }
  132. }
  133. [MonoTODO]
  134. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  135. [OdbcDescriptionAttribute ("Current ODBC Driver")]
  136. public string Driver {
  137. get {
  138. throw new NotImplementedException ();
  139. }
  140. }
  141. [MonoTODO]
  142. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  143. [OdbcDescriptionAttribute ("Version of the product accessed by the ODBC Driver")]
  144. [BrowsableAttribute (false)]
  145. public string ServerVersion {
  146. get {
  147. throw new NotImplementedException ();
  148. }
  149. }
  150. #endregion // Properties
  151. #region Methods
  152. public OdbcTransaction BeginTransaction ()
  153. {
  154. return BeginTransaction(IsolationLevel.Unspecified);
  155. }
  156. IDbTransaction IDbConnection.BeginTransaction ()
  157. {
  158. return (IDbTransaction) BeginTransaction();
  159. }
  160. public OdbcTransaction BeginTransaction (IsolationLevel level)
  161. {
  162. if (transaction==null)
  163. {
  164. transaction=new OdbcTransaction(this,level);
  165. return transaction;
  166. }
  167. else
  168. throw new InvalidOperationException();
  169. }
  170. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  171. {
  172. return (IDbTransaction) BeginTransaction(level);
  173. }
  174. public void Close ()
  175. {
  176. if (State == ConnectionState.Open) {
  177. // TODO: Free handles
  178. hdbc = IntPtr.Zero;
  179. transaction=null;
  180. }
  181. else
  182. throw new InvalidOperationException();
  183. }
  184. public OdbcCommand CreateCommand ()
  185. {
  186. return new OdbcCommand("", this, transaction);
  187. }
  188. [MonoTODO]
  189. public void ChangeDatabase(string Database)
  190. {
  191. throw new NotImplementedException ();
  192. }
  193. [MonoTODO]
  194. protected override void Dispose (bool disposing)
  195. {
  196. }
  197. [MonoTODO]
  198. object ICloneable.Clone ()
  199. {
  200. throw new NotImplementedException();
  201. }
  202. IDbCommand IDbConnection.CreateCommand ()
  203. {
  204. return (IDbCommand) CreateCommand ();
  205. }
  206. public void Open ()
  207. {
  208. if (State == ConnectionState.Open)
  209. throw new InvalidOperationException ();
  210. // allocate connection handle
  211. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Dbc, henv, ref hdbc);
  212. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  213. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Env,henv));
  214. // DSN connection
  215. if (connectionString.ToLower().IndexOf("dsn=")>=0)
  216. {
  217. string _uid="", _pwd="", _dsn="";
  218. string[] items=connectionString.Split(new char[1]{';'});
  219. foreach (string item in items)
  220. {
  221. string[] parts=item.Split(new char[1] {'='});
  222. switch (parts[0].Trim().ToLower())
  223. {
  224. case "dsn":
  225. _dsn=parts[1].Trim();
  226. break;
  227. case "uid":
  228. _uid=parts[1].Trim();
  229. break;
  230. case "pwd":
  231. _pwd=parts[1].Trim();
  232. break;
  233. }
  234. }
  235. ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
  236. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  237. throw new OdbcException(new OdbcError("SQLConnect",OdbcHandleType.Dbc,hdbc));
  238. }
  239. else
  240. {
  241. // DSN-less Connection
  242. string OutConnectionString=new String(' ',1024);
  243. short OutLen=0;
  244. ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, connectionString, -3,
  245. OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
  246. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  247. throw new OdbcException(new OdbcError("SQLDriverConnect",OdbcHandleType.Dbc,hdbc));
  248. }
  249. }
  250. [MonoTODO]
  251. public static void ReleaseObjectPool ()
  252. {
  253. throw new NotImplementedException ();
  254. }
  255. [MonoTODO]
  256. public void EnlistDistributedTransaction ( ITransaction transaction) {
  257. throw new NotImplementedException ();
  258. }
  259. #endregion
  260. #region Events and Delegates
  261. [OdbcDescription ("DbConnection_StateChange")]
  262. [OdbcCategory ("DataCategory_StateChange")]
  263. public event StateChangeEventHandler StateChange;
  264. [OdbcDescription ("DbConnection_InfoMessage")]
  265. [OdbcCategory ("DataCategory_InfoMessage")]
  266. public event OdbcInfoMessageEventHandler InfoMessage;
  267. #endregion
  268. }
  269. }