SqlConnection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // System.Data.SqlClient.SqlConnection
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Data;
  31. using System.Data.Common;
  32. using System.Collections;
  33. using System.Data.ProviderBase;
  34. using java.sql;
  35. using System.Configuration;
  36. using Mainsoft.Data.Configuration;
  37. using Mainsoft.Data.Jdbc.Providers;
  38. namespace System.Data.SqlClient
  39. {
  40. public class SqlConnection : AbstractDBConnection
  41. {
  42. #region Fields
  43. private const int DEFAULT_PACKET_SIZE = 8192;
  44. #endregion // Fields
  45. #region Constructors
  46. public SqlConnection() : this(null)
  47. {
  48. }
  49. public SqlConnection(String connectionString) : base(connectionString)
  50. {
  51. }
  52. #endregion // Constructors
  53. #region Events
  54. [DataCategory ("InfoMessage")]
  55. [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
  56. public event SqlInfoMessageEventHandler InfoMessage;
  57. #endregion // Events
  58. #region Properties
  59. public string WorkstationId
  60. {
  61. get { return (string)ConnectionStringBuilder["workstation id"]; }
  62. }
  63. public int PacketSize
  64. {
  65. get {
  66. string packetSize = (string)ConnectionStringBuilder["Packet Size"];
  67. if (packetSize == null || packetSize.Length == 0) {
  68. return DEFAULT_PACKET_SIZE;
  69. }
  70. try {
  71. return Convert.ToInt32(packetSize);
  72. }
  73. catch(FormatException e) {
  74. throw ExceptionHelper.InvalidValueForKey("packet size");
  75. }
  76. catch (OverflowException e) {
  77. throw ExceptionHelper.InvalidValueForKey("packet size");
  78. }
  79. }
  80. }
  81. protected override IConnectionProvider GetConnectionProvider() {
  82. IDictionary conProviderDict = ConnectionStringDictionary.Parse(ConnectionString);
  83. string provider = (string)conProviderDict["Provider"];
  84. if (provider == null)
  85. provider = "SQLCLIENT";
  86. return GetConnectionProvider("Mainsoft.Data.Configuration/SqlClientProviders", provider);
  87. }
  88. #endregion // Properties
  89. #region Methods
  90. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
  91. return BeginTransaction(isolationLevel);
  92. }
  93. public SqlTransaction BeginTransaction(String transactionName)
  94. {
  95. return BeginTransaction(IsolationLevel.ReadCommitted,transactionName);
  96. }
  97. public new SqlTransaction BeginTransaction(IsolationLevel isolationLevel)
  98. {
  99. return BeginTransaction(isolationLevel,"Transaction");
  100. }
  101. public new SqlTransaction BeginTransaction()
  102. {
  103. return BeginTransaction(IsolationLevel.ReadCommitted);
  104. }
  105. public SqlTransaction BeginTransaction(IsolationLevel isolationLevel, string transactionName)
  106. {
  107. return new SqlTransaction(isolationLevel, this, transactionName);
  108. }
  109. public new SqlCommand CreateCommand()
  110. {
  111. return new SqlCommand(this);
  112. }
  113. protected override DbCommand CreateDbCommand() {
  114. return CreateCommand();
  115. }
  116. protected internal sealed override void OnSqlWarning(SQLWarning warning)
  117. {
  118. SqlErrorCollection col = new SqlErrorCollection(warning, this);
  119. OnSqlInfoMessage(new SqlInfoMessageEventArgs(col));
  120. }
  121. protected sealed override SystemException CreateException(SQLException e)
  122. {
  123. return new SqlException(e, this);
  124. }
  125. protected sealed override SystemException CreateException(string message)
  126. {
  127. return new SqlException(message, null, this);
  128. }
  129. private void OnSqlInfoMessage (SqlInfoMessageEventArgs value)
  130. {
  131. if (InfoMessage != null) {
  132. InfoMessage (this, value);
  133. }
  134. }
  135. #if NET_2_0
  136. [MonoNotSupported("")]
  137. public static void ChangePassword (string connectionString, string newPassword)
  138. {
  139. throw new NotImplementedException ();
  140. // FIXME: refactored from Mono implementation. Not finished!!!
  141. if (connectionString == null || newPassword == null || newPassword == String.Empty)
  142. throw new ArgumentNullException ();
  143. if (newPassword.Length > 128)
  144. throw new ArgumentException ("The value of newPassword exceeds its permittable length which is 128");
  145. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder (connectionString);
  146. if (builder.IntegratedSecurity) {
  147. throw new ArgumentException ("Can't use integrated security when changing password");
  148. }
  149. using (SqlConnection conn = new SqlConnection (connectionString)) {
  150. conn.Open ();
  151. SqlCommand cmd = conn.CreateCommand ();
  152. cmd.CommandText = "sp_password";
  153. cmd.CommandType = CommandType.StoredProcedure;
  154. // FIXME: Need to extract old password and user from our structures
  155. // of the connectionString.
  156. cmd.Parameters.Add (builder.Password); // Is this good???
  157. cmd.Parameters.Add (newPassword);
  158. cmd.Parameters.Add (builder.UserID); // Is this good???
  159. cmd.ExecuteNonQuery();
  160. }
  161. }
  162. #region Pooling
  163. [MonoNotSupported("Pooling not supported")]
  164. public static void ClearPool (SqlConnection connection)
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. [MonoNotSupported ("Pooling not supported")]
  169. public static void ClearAllPools ()
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. #endregion
  174. #region Statistics
  175. [MonoNotSupported ("Statistics not supported")]
  176. public IDictionary RetrieveStatistics ()
  177. {
  178. throw new NotImplementedException ();
  179. }
  180. [MonoNotSupported ("Statistics not supported")]
  181. public void ResetStatistics ()
  182. {
  183. throw new NotImplementedException ();
  184. }
  185. #endregion
  186. #endif
  187. #endregion // Methods
  188. }
  189. }