SqlConnection.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. static readonly IConnectionProvider _connectionProvider;
  45. #endregion // Fields
  46. #region Constructors
  47. static SqlConnection() {
  48. IDictionary providerInfo = (IDictionary)((IList) ConfigurationSettings.GetConfig("Mainsoft.Data.Configuration/SqlClientProvider"))[0];
  49. string providerType = (string) providerInfo [ConfigurationConsts.ProviderType];
  50. if (providerType == null || providerType.Length == 0)
  51. _connectionProvider = new GenericProvider (providerInfo);
  52. else {
  53. Type t = Type.GetType (providerType);
  54. _connectionProvider = (IConnectionProvider) Activator.CreateInstance (t , new object[] {providerInfo});
  55. }
  56. }
  57. public SqlConnection() : this(null)
  58. {
  59. }
  60. public SqlConnection(String connectionString) : base(connectionString)
  61. {
  62. }
  63. #endregion // Constructors
  64. #region Events
  65. [DataCategory ("InfoMessage")]
  66. [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
  67. public event SqlInfoMessageEventHandler InfoMessage;
  68. [DataCategory ("StateChange")]
  69. [DataSysDescription ("Event triggered when the connection changes state.")]
  70. public event StateChangeEventHandler StateChange;
  71. #endregion // Events
  72. #region Properties
  73. public string WorkstationId
  74. {
  75. get { return (string)ConnectionStringBuilder["workstation id"]; }
  76. }
  77. public int PacketSize
  78. {
  79. get {
  80. string packetSize = (string)ConnectionStringBuilder["Packet Size"];
  81. if (packetSize == null || packetSize.Length == 0) {
  82. return DEFAULT_PACKET_SIZE;
  83. }
  84. try {
  85. return Convert.ToInt32(packetSize);
  86. }
  87. catch(FormatException e) {
  88. throw ExceptionHelper.InvalidValueForKey("packet size");
  89. }
  90. catch (OverflowException e) {
  91. throw ExceptionHelper.InvalidValueForKey("packet size");
  92. }
  93. }
  94. }
  95. protected override IConnectionProvider GetConnectionProvider() {
  96. return _connectionProvider;
  97. }
  98. #endregion // Properties
  99. #region Methods
  100. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
  101. return BeginTransaction(isolationLevel);
  102. }
  103. public SqlTransaction BeginTransaction(String transactionName)
  104. {
  105. return BeginTransaction(IsolationLevel.ReadCommitted,transactionName);
  106. }
  107. public new SqlTransaction BeginTransaction(IsolationLevel isolationLevel)
  108. {
  109. return BeginTransaction(isolationLevel,"Transaction");
  110. }
  111. public new SqlTransaction BeginTransaction()
  112. {
  113. return BeginTransaction(IsolationLevel.ReadCommitted);
  114. }
  115. public SqlTransaction BeginTransaction(IsolationLevel isolationLevel, string transactionName)
  116. {
  117. return new SqlTransaction(isolationLevel, this, transactionName);
  118. }
  119. public new SqlCommand CreateCommand()
  120. {
  121. return new SqlCommand(this);
  122. }
  123. protected override DbCommand CreateDbCommand() {
  124. return CreateCommand();
  125. }
  126. protected internal sealed override void OnSqlWarning(SQLWarning warning)
  127. {
  128. SqlErrorCollection col = new SqlErrorCollection(warning, this);
  129. OnSqlInfoMessage(new SqlInfoMessageEventArgs(col));
  130. }
  131. protected sealed override SystemException CreateException(SQLException e)
  132. {
  133. return new SqlException(e, this);
  134. }
  135. protected sealed override SystemException CreateException(string message)
  136. {
  137. return new SqlException(message, null, this);
  138. }
  139. private void OnSqlInfoMessage (SqlInfoMessageEventArgs value)
  140. {
  141. if (InfoMessage != null) {
  142. InfoMessage (this, value);
  143. }
  144. }
  145. protected internal sealed override void OnStateChanged(ConnectionState orig, ConnectionState current)
  146. {
  147. if(StateChange != null) {
  148. StateChange(this, new StateChangeEventArgs(orig, current));
  149. }
  150. }
  151. public override void Close()
  152. {
  153. ConnectionState orig = State;
  154. base.Close();
  155. ConnectionState current = State;
  156. if(current != orig) {
  157. OnStateChanged(orig, current);
  158. }
  159. }
  160. #endregion // Methods
  161. }
  162. }