OdbcConnection.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. bool disposed = false;
  46. #endregion
  47. #region Constructors
  48. public OdbcConnection ()
  49. {
  50. connectionTimeout = 15;
  51. connectionString = null;
  52. }
  53. public OdbcConnection (string connectionString) : this ()
  54. {
  55. ConnectionString = connectionString;
  56. }
  57. #endregion // Constructors
  58. #region Properties
  59. internal IntPtr hDbc
  60. {
  61. get { return hdbc; }
  62. }
  63. [OdbcCategoryAttribute ("DataCategory_Data")]
  64. [DefaultValue ("")]
  65. [OdbcDescriptionAttribute ("Information used to connect to a Data Source")]
  66. [RefreshPropertiesAttribute (RefreshProperties.All)]
  67. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  68. [RecommendedAsConfigurableAttribute (true)]
  69. public string ConnectionString {
  70. get {
  71. return connectionString;
  72. }
  73. set {
  74. connectionString = value;
  75. }
  76. }
  77. [OdbcDescriptionAttribute ("Current connection timeout value, not settable in the ConnectionString")]
  78. [DefaultValue (15)]
  79. public int ConnectionTimeout {
  80. get {
  81. return connectionTimeout;
  82. }
  83. set {
  84. if (value < 0) {
  85. throw new ArgumentException("Timout should not be less than zero.");
  86. }
  87. connectionTimeout = value;
  88. }
  89. }
  90. // public string DataSource {
  91. // get {
  92. // if (State==ConnectionState.Open)
  93. // return _dsn;
  94. // else
  95. // return null;
  96. // }
  97. // }
  98. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  99. [OdbcDescriptionAttribute ("Current data source Catlog value, 'Database=X' in the ConnectionString")]
  100. public string Database {
  101. get {
  102. return "";
  103. }
  104. }
  105. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  106. [OdbcDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed")]
  107. [BrowsableAttribute (false)]
  108. public ConnectionState State
  109. {
  110. get {
  111. if (hdbc!=IntPtr.Zero) {
  112. return ConnectionState.Open;
  113. }
  114. else
  115. return ConnectionState.Closed;
  116. }
  117. }
  118. [MonoTODO]
  119. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  120. [OdbcDescriptionAttribute ("Current data source, 'Server=X' in the ConnectionString")]
  121. public string DataSource {
  122. get {
  123. throw new NotImplementedException ();
  124. }
  125. }
  126. [MonoTODO]
  127. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  128. [OdbcDescriptionAttribute ("Current ODBC Driver")]
  129. public string Driver {
  130. get {
  131. throw new NotImplementedException ();
  132. }
  133. }
  134. [MonoTODO]
  135. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  136. [OdbcDescriptionAttribute ("Version of the product accessed by the ODBC Driver")]
  137. [BrowsableAttribute (false)]
  138. public string ServerVersion {
  139. get {
  140. throw new NotImplementedException ();
  141. }
  142. }
  143. #endregion // Properties
  144. #region Methods
  145. public OdbcTransaction BeginTransaction ()
  146. {
  147. return BeginTransaction(IsolationLevel.Unspecified);
  148. }
  149. IDbTransaction IDbConnection.BeginTransaction ()
  150. {
  151. return (IDbTransaction) BeginTransaction();
  152. }
  153. public OdbcTransaction BeginTransaction (IsolationLevel level)
  154. {
  155. if (transaction==null)
  156. {
  157. transaction=new OdbcTransaction(this,level);
  158. return transaction;
  159. }
  160. else
  161. throw new InvalidOperationException();
  162. }
  163. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
  164. {
  165. return (IDbTransaction) BeginTransaction(level);
  166. }
  167. public void Close ()
  168. {
  169. OdbcReturn ret = OdbcReturn.Error;
  170. if (State == ConnectionState.Open) {
  171. // disconnect
  172. ret = libodbc.SQLDisconnect (hdbc);
  173. if ( (ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  174. throw new OdbcException (new OdbcError ("SQLDisconnect", OdbcHandleType.Dbc,hdbc));
  175. // free handles
  176. if (hdbc != IntPtr.Zero) {
  177. ret = libodbc.SQLFreeHandle ( (ushort) OdbcHandleType.Dbc, hdbc);
  178. if ( (ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  179. throw new OdbcException (new OdbcError ("SQLFreeHandle", OdbcHandleType.Dbc,hdbc));
  180. }
  181. hdbc = IntPtr.Zero;
  182. if (henv != IntPtr.Zero) {
  183. ret = libodbc.SQLFreeHandle ( (ushort) OdbcHandleType.Env, henv);
  184. if ( (ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  185. throw new OdbcException (new OdbcError ("SQLFreeHandle", OdbcHandleType.Env,henv));
  186. }
  187. henv = IntPtr.Zero;
  188. transaction = null;
  189. }
  190. }
  191. public OdbcCommand CreateCommand ()
  192. {
  193. return new OdbcCommand("", this, transaction);
  194. }
  195. [MonoTODO]
  196. public void ChangeDatabase(string Database)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. protected override void Dispose (bool disposing)
  201. {
  202. if (!this.disposed) {
  203. try
  204. {
  205. // release the native unmananged resources
  206. this.Close();
  207. this.disposed = true;
  208. }
  209. finally
  210. {
  211. // call Dispose on the base class
  212. base.Dispose(disposing);
  213. }
  214. }
  215. }
  216. [MonoTODO]
  217. object ICloneable.Clone ()
  218. {
  219. throw new NotImplementedException();
  220. }
  221. IDbCommand IDbConnection.CreateCommand ()
  222. {
  223. return (IDbCommand) CreateCommand ();
  224. }
  225. public void Open ()
  226. {
  227. if (State == ConnectionState.Open)
  228. throw new InvalidOperationException ();
  229. OdbcReturn ret = OdbcReturn.Error;
  230. // allocate Environment handle
  231. ret = libodbc.SQLAllocHandle (OdbcHandleType.Env, IntPtr.Zero, ref henv);
  232. if ( (ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  233. throw new OdbcException (new OdbcError ("SQLAllocHandle"));
  234. ret=libodbc.SQLSetEnvAttr (henv, OdbcEnv.OdbcVersion, (IntPtr) libodbc.SQL_OV_ODBC3 , 0);
  235. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  236. throw new OdbcException (new OdbcError ("SQLSetEnvAttr", OdbcHandleType.Env,henv));
  237. // allocate connection handle
  238. ret=libodbc.SQLAllocHandle (OdbcHandleType.Dbc, henv, ref hdbc);
  239. if ( (ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  240. throw new OdbcException (new OdbcError ("SQLAllocHandle",OdbcHandleType.Env,henv));
  241. // DSN connection
  242. if (connectionString.ToLower().IndexOf("dsn=")>=0)
  243. {
  244. string _uid="", _pwd="", _dsn="";
  245. string[] items=connectionString.Split(new char[1]{';'});
  246. foreach (string item in items)
  247. {
  248. string[] parts=item.Split(new char[1] {'='});
  249. switch (parts[0].Trim().ToLower())
  250. {
  251. case "dsn":
  252. _dsn=parts[1].Trim();
  253. break;
  254. case "uid":
  255. _uid=parts[1].Trim();
  256. break;
  257. case "pwd":
  258. _pwd=parts[1].Trim();
  259. break;
  260. }
  261. }
  262. ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
  263. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  264. throw new OdbcException(new OdbcError("SQLConnect",OdbcHandleType.Dbc,hdbc));
  265. }
  266. else
  267. {
  268. // DSN-less Connection
  269. string OutConnectionString=new String(' ',1024);
  270. short OutLen=0;
  271. ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, connectionString, -3,
  272. OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
  273. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  274. throw new OdbcException(new OdbcError("SQLDriverConnect",OdbcHandleType.Dbc,hdbc));
  275. }
  276. }
  277. [MonoTODO]
  278. public static void ReleaseObjectPool ()
  279. {
  280. throw new NotImplementedException ();
  281. }
  282. [MonoTODO]
  283. public void EnlistDistributedTransaction ( ITransaction transaction) {
  284. throw new NotImplementedException ();
  285. }
  286. #endregion
  287. #region Events and Delegates
  288. [OdbcDescription ("DbConnection_StateChange")]
  289. [OdbcCategory ("DataCategory_StateChange")]
  290. public event StateChangeEventHandler StateChange;
  291. [OdbcDescription ("DbConnection_InfoMessage")]
  292. [OdbcCategory ("DataCategory_InfoMessage")]
  293. public event OdbcInfoMessageEventHandler InfoMessage;
  294. #endregion
  295. }
  296. }