SqlConnection.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. [DataCategory ("StateChange")]
  58. [DataSysDescription ("Event triggered when the connection changes state.")]
  59. public event StateChangeEventHandler StateChange;
  60. #endregion // Events
  61. #region Properties
  62. public string WorkstationId
  63. {
  64. get { return (string)ConnectionStringBuilder["workstation id"]; }
  65. }
  66. public int PacketSize
  67. {
  68. get {
  69. string packetSize = (string)ConnectionStringBuilder["Packet Size"];
  70. if (packetSize == null || packetSize.Length == 0) {
  71. return DEFAULT_PACKET_SIZE;
  72. }
  73. try {
  74. return Convert.ToInt32(packetSize);
  75. }
  76. catch(FormatException e) {
  77. throw ExceptionHelper.InvalidValueForKey("packet size");
  78. }
  79. catch (OverflowException e) {
  80. throw ExceptionHelper.InvalidValueForKey("packet size");
  81. }
  82. }
  83. }
  84. protected override IConnectionProvider GetConnectionProvider() {
  85. IDictionary conProviderDict = ConnectionStringDictionary.Parse(ConnectionString);
  86. string provider = (string)conProviderDict["Provider"];
  87. if (provider == null)
  88. provider = "SQLCLIENT";
  89. return GetConnectionProvider("Mainsoft.Data.Configuration/SqlClientProviders", provider);
  90. }
  91. #endregion // Properties
  92. #region Methods
  93. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
  94. return BeginTransaction(isolationLevel);
  95. }
  96. public SqlTransaction BeginTransaction(String transactionName)
  97. {
  98. return BeginTransaction(IsolationLevel.ReadCommitted,transactionName);
  99. }
  100. public new SqlTransaction BeginTransaction(IsolationLevel isolationLevel)
  101. {
  102. return BeginTransaction(isolationLevel,"Transaction");
  103. }
  104. public new SqlTransaction BeginTransaction()
  105. {
  106. return BeginTransaction(IsolationLevel.ReadCommitted);
  107. }
  108. public SqlTransaction BeginTransaction(IsolationLevel isolationLevel, string transactionName)
  109. {
  110. return new SqlTransaction(isolationLevel, this, transactionName);
  111. }
  112. public new SqlCommand CreateCommand()
  113. {
  114. return new SqlCommand(this);
  115. }
  116. protected override DbCommand CreateDbCommand() {
  117. return CreateCommand();
  118. }
  119. protected internal sealed override void OnSqlWarning(SQLWarning warning)
  120. {
  121. SqlErrorCollection col = new SqlErrorCollection(warning, this);
  122. OnSqlInfoMessage(new SqlInfoMessageEventArgs(col));
  123. }
  124. protected sealed override SystemException CreateException(SQLException e)
  125. {
  126. return new SqlException(e, this);
  127. }
  128. protected sealed override SystemException CreateException(string message)
  129. {
  130. return new SqlException(message, null, this);
  131. }
  132. private void OnSqlInfoMessage (SqlInfoMessageEventArgs value)
  133. {
  134. if (InfoMessage != null) {
  135. InfoMessage (this, value);
  136. }
  137. }
  138. protected internal sealed override void OnStateChanged(ConnectionState orig, ConnectionState current)
  139. {
  140. if(StateChange != null) {
  141. StateChange(this, new StateChangeEventArgs(orig, current));
  142. }
  143. }
  144. public override void Close()
  145. {
  146. ConnectionState orig = State;
  147. base.Close();
  148. ConnectionState current = State;
  149. if(current != orig) {
  150. OnStateChanged(orig, current);
  151. }
  152. }
  153. #endregion // Methods
  154. }
  155. }