AutoClosingSqlCommand.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 OpenSimulator 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.Data;
  28. using System.Data.SqlClient;
  29. namespace OpenSim.Data.MSSQL
  30. {
  31. /// <summary>
  32. /// Encapsulates a SqlCommand object but ensures that when it is disposed, its connection is closed and disposed also.
  33. /// </summary>
  34. internal class AutoClosingSqlCommand : IDbCommand
  35. {
  36. private SqlCommand realCommand;
  37. public AutoClosingSqlCommand(SqlCommand cmd)
  38. {
  39. realCommand = cmd;
  40. }
  41. #region IDbCommand Members
  42. public void Cancel()
  43. {
  44. realCommand.Cancel();
  45. }
  46. public string CommandText
  47. {
  48. get
  49. {
  50. return realCommand.CommandText;
  51. }
  52. set
  53. {
  54. realCommand.CommandText = value;
  55. }
  56. }
  57. public int CommandTimeout
  58. {
  59. get
  60. {
  61. return realCommand.CommandTimeout;
  62. }
  63. set
  64. {
  65. realCommand.CommandTimeout = value;
  66. }
  67. }
  68. public CommandType CommandType
  69. {
  70. get
  71. {
  72. return realCommand.CommandType;
  73. }
  74. set
  75. {
  76. realCommand.CommandType = value;
  77. }
  78. }
  79. IDbConnection IDbCommand.Connection
  80. {
  81. get
  82. {
  83. return realCommand.Connection;
  84. }
  85. set
  86. {
  87. realCommand.Connection = (SqlConnection) value;
  88. }
  89. }
  90. public SqlConnection Connection
  91. {
  92. get
  93. {
  94. return realCommand.Connection;
  95. }
  96. }
  97. IDbDataParameter IDbCommand.CreateParameter()
  98. {
  99. return realCommand.CreateParameter();
  100. }
  101. public SqlParameter CreateParameter()
  102. {
  103. return realCommand.CreateParameter();
  104. }
  105. public int ExecuteNonQuery()
  106. {
  107. return realCommand.ExecuteNonQuery();
  108. }
  109. IDataReader IDbCommand.ExecuteReader(CommandBehavior behavior)
  110. {
  111. return realCommand.ExecuteReader(behavior);
  112. }
  113. public SqlDataReader ExecuteReader(CommandBehavior behavior)
  114. {
  115. return realCommand.ExecuteReader(behavior);
  116. }
  117. IDataReader IDbCommand.ExecuteReader()
  118. {
  119. return realCommand.ExecuteReader();
  120. }
  121. public SqlDataReader ExecuteReader()
  122. {
  123. return realCommand.ExecuteReader();
  124. }
  125. public object ExecuteScalar()
  126. {
  127. return realCommand.ExecuteScalar();
  128. }
  129. IDataParameterCollection IDbCommand.Parameters
  130. {
  131. get { return realCommand.Parameters; }
  132. }
  133. public SqlParameterCollection Parameters
  134. {
  135. get { return realCommand.Parameters; }
  136. }
  137. public void Prepare()
  138. {
  139. realCommand.Prepare();
  140. }
  141. // IDbTransaction IDbCommand.Transaction
  142. // {
  143. // get
  144. // {
  145. // return realCommand.Transaction;
  146. // }
  147. // set
  148. // {
  149. // realCommand.Transaction = (SqlTransaction) value;
  150. // }
  151. // }
  152. public IDbTransaction Transaction
  153. {
  154. get { return realCommand.Transaction; }
  155. set { realCommand.Transaction = (SqlTransaction)value; }
  156. }
  157. UpdateRowSource IDbCommand.UpdatedRowSource
  158. {
  159. get
  160. {
  161. return realCommand.UpdatedRowSource;
  162. }
  163. set
  164. {
  165. realCommand.UpdatedRowSource = value;
  166. }
  167. }
  168. #endregion
  169. #region IDisposable Members
  170. public void Dispose()
  171. {
  172. SqlConnection conn = realCommand.Connection;
  173. try
  174. {
  175. realCommand.Dispose();
  176. }
  177. finally
  178. {
  179. try
  180. {
  181. conn.Close();
  182. }
  183. finally
  184. {
  185. conn.Dispose();
  186. }
  187. }
  188. }
  189. #endregion
  190. }
  191. }