AutoClosingSqlCommand.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using System.Data.SqlClient;
  31. using System.Data;
  32. namespace OpenSim.Data.MSSQL
  33. {
  34. /// <summary>
  35. /// Encapsulates a SqlCommand object but ensures that when it is disposed, its connection is closed and disposed also.
  36. /// </summary>
  37. internal class AutoClosingSqlCommand : IDbCommand
  38. {
  39. private SqlCommand realCommand;
  40. public AutoClosingSqlCommand(SqlCommand cmd)
  41. {
  42. realCommand = cmd;
  43. }
  44. #region IDbCommand Members
  45. public void Cancel()
  46. {
  47. realCommand.Cancel();
  48. }
  49. public string CommandText
  50. {
  51. get
  52. {
  53. return realCommand.CommandText;
  54. }
  55. set
  56. {
  57. realCommand.CommandText = value;
  58. }
  59. }
  60. public int CommandTimeout
  61. {
  62. get
  63. {
  64. return realCommand.CommandTimeout;
  65. }
  66. set
  67. {
  68. realCommand.CommandTimeout = value;
  69. }
  70. }
  71. public CommandType CommandType
  72. {
  73. get
  74. {
  75. return realCommand.CommandType;
  76. }
  77. set
  78. {
  79. realCommand.CommandType = value;
  80. }
  81. }
  82. IDbConnection IDbCommand.Connection
  83. {
  84. get
  85. {
  86. return realCommand.Connection;
  87. }
  88. set
  89. {
  90. realCommand.Connection = (SqlConnection) value;
  91. }
  92. }
  93. public SqlConnection Connection
  94. {
  95. get
  96. {
  97. return realCommand.Connection;
  98. }
  99. }
  100. IDbDataParameter IDbCommand.CreateParameter()
  101. {
  102. return realCommand.CreateParameter();
  103. }
  104. public SqlParameter CreateParameter()
  105. {
  106. return realCommand.CreateParameter();
  107. }
  108. public int ExecuteNonQuery()
  109. {
  110. return realCommand.ExecuteNonQuery();
  111. }
  112. IDataReader IDbCommand.ExecuteReader(CommandBehavior behavior)
  113. {
  114. return realCommand.ExecuteReader(behavior);
  115. }
  116. public SqlDataReader ExecuteReader(CommandBehavior behavior)
  117. {
  118. return realCommand.ExecuteReader(behavior);
  119. }
  120. IDataReader IDbCommand.ExecuteReader()
  121. {
  122. return realCommand.ExecuteReader();
  123. }
  124. public SqlDataReader ExecuteReader()
  125. {
  126. return realCommand.ExecuteReader();
  127. }
  128. public object ExecuteScalar()
  129. {
  130. return realCommand.ExecuteScalar();
  131. }
  132. IDataParameterCollection IDbCommand.Parameters
  133. {
  134. get { return realCommand.Parameters; }
  135. }
  136. public SqlParameterCollection Parameters
  137. {
  138. get { return realCommand.Parameters; }
  139. }
  140. public void Prepare()
  141. {
  142. realCommand.Prepare();
  143. }
  144. IDbTransaction IDbCommand.Transaction
  145. {
  146. get
  147. {
  148. return realCommand.Transaction;
  149. }
  150. set
  151. {
  152. realCommand.Transaction = (SqlTransaction) value;
  153. }
  154. }
  155. UpdateRowSource IDbCommand.UpdatedRowSource
  156. {
  157. get
  158. {
  159. return realCommand.UpdatedRowSource;
  160. }
  161. set
  162. {
  163. realCommand.UpdatedRowSource = value;
  164. }
  165. }
  166. #endregion
  167. #region IDisposable Members
  168. public void Dispose()
  169. {
  170. SqlConnection conn = realCommand.Connection;
  171. try
  172. {
  173. realCommand.Dispose();
  174. }
  175. finally
  176. {
  177. try
  178. {
  179. conn.Close();
  180. }
  181. finally
  182. {
  183. conn.Dispose();
  184. }
  185. }
  186. }
  187. #endregion
  188. }
  189. }