DBCommand.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DbCommand.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.Common {
  9. using System;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Threading.Tasks;
  13. using System.Threading;
  14. public abstract class DbCommand : Component, IDbCommand { // V1.2.3300
  15. protected DbCommand() : base() {
  16. }
  17. [
  18. DefaultValue(""),
  19. RefreshProperties(RefreshProperties.All),
  20. ResCategoryAttribute(Res.DataCategory_Data),
  21. ResDescriptionAttribute(Res.DbCommand_CommandText),
  22. ]
  23. abstract public string CommandText {
  24. get;
  25. set;
  26. }
  27. [
  28. ResCategoryAttribute(Res.DataCategory_Data),
  29. ResDescriptionAttribute(Res.DbCommand_CommandTimeout),
  30. ]
  31. abstract public int CommandTimeout {
  32. get;
  33. set;
  34. }
  35. [
  36. DefaultValue(System.Data.CommandType.Text),
  37. RefreshProperties(RefreshProperties.All),
  38. ResCategoryAttribute(Res.DataCategory_Data),
  39. ResDescriptionAttribute(Res.DbCommand_CommandType),
  40. ]
  41. abstract public CommandType CommandType {
  42. get;
  43. set;
  44. }
  45. [
  46. Browsable(false),
  47. DefaultValue(null),
  48. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  49. ResCategoryAttribute(Res.DataCategory_Data),
  50. ResDescriptionAttribute(Res.DbCommand_Connection),
  51. ]
  52. public DbConnection Connection {
  53. get {
  54. return DbConnection;
  55. }
  56. set {
  57. DbConnection = value;
  58. }
  59. }
  60. IDbConnection IDbCommand.Connection {
  61. get {
  62. return DbConnection;
  63. }
  64. set {
  65. DbConnection = (DbConnection)value;
  66. }
  67. }
  68. abstract protected DbConnection DbConnection { // V1.2.3300
  69. get;
  70. set;
  71. }
  72. abstract protected DbParameterCollection DbParameterCollection { // V1.2.3300
  73. get;
  74. }
  75. abstract protected DbTransaction DbTransaction { // V1.2.3300
  76. get;
  77. set;
  78. }
  79. // @devnote: By default, the cmd object is visible on the design surface (i.e. VS7 Server Tray)
  80. // to limit the number of components that clutter the design surface,
  81. // when the DataAdapter design wizard generates the insert/update/delete commands it will
  82. // set the DesignTimeVisible property to false so that cmds won't appear as individual objects
  83. [
  84. DefaultValue(true),
  85. DesignOnly(true),
  86. Browsable(false),
  87. EditorBrowsableAttribute(EditorBrowsableState.Never),
  88. ]
  89. public abstract bool DesignTimeVisible {
  90. get;
  91. set;
  92. }
  93. [
  94. Browsable(false),
  95. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  96. ResCategoryAttribute(Res.DataCategory_Data),
  97. ResDescriptionAttribute(Res.DbCommand_Parameters),
  98. ]
  99. public DbParameterCollection Parameters {
  100. get {
  101. return DbParameterCollection;
  102. }
  103. }
  104. IDataParameterCollection IDbCommand.Parameters {
  105. get {
  106. return (DbParameterCollection)DbParameterCollection;
  107. }
  108. }
  109. [
  110. Browsable(false),
  111. DefaultValue(null),
  112. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  113. ResDescriptionAttribute(Res.DbCommand_Transaction),
  114. ]
  115. public DbTransaction Transaction {
  116. get {
  117. return DbTransaction;
  118. }
  119. set {
  120. DbTransaction = value;
  121. }
  122. }
  123. IDbTransaction IDbCommand.Transaction {
  124. get {
  125. return DbTransaction;
  126. }
  127. set {
  128. DbTransaction = (DbTransaction)value;
  129. }
  130. }
  131. [
  132. DefaultValue(System.Data.UpdateRowSource.Both),
  133. ResCategoryAttribute(Res.DataCategory_Update),
  134. ResDescriptionAttribute(Res.DbCommand_UpdatedRowSource),
  135. ]
  136. abstract public UpdateRowSource UpdatedRowSource {
  137. get;
  138. set;
  139. }
  140. internal void CancelIgnoreFailure() {
  141. // This method is used to route CancellationTokens to the Cancel method.
  142. // Cancellation is a suggestion, and exceptions should be ignored
  143. // rather than allowed to be unhandled, as there is no way to route
  144. // them to the caller. It would be expected that the error will be
  145. // observed anyway from the regular method. An example is cancelling
  146. // an operation on a closed connection.
  147. try
  148. {
  149. Cancel();
  150. } catch(Exception) {
  151. }
  152. }
  153. abstract public void Cancel();
  154. public DbParameter CreateParameter(){ // V1.2.3300
  155. return CreateDbParameter();
  156. }
  157. IDbDataParameter IDbCommand.CreateParameter() { // V1.2.3300
  158. return CreateDbParameter();
  159. }
  160. abstract protected DbParameter CreateDbParameter();
  161. abstract protected DbDataReader ExecuteDbDataReader(CommandBehavior behavior);
  162. abstract public int ExecuteNonQuery();
  163. public DbDataReader ExecuteReader() {
  164. return (DbDataReader)ExecuteDbDataReader(CommandBehavior.Default);
  165. }
  166. IDataReader IDbCommand.ExecuteReader() {
  167. return (DbDataReader)ExecuteDbDataReader(CommandBehavior.Default);
  168. }
  169. public DbDataReader ExecuteReader(CommandBehavior behavior){
  170. return (DbDataReader)ExecuteDbDataReader(behavior);
  171. }
  172. IDataReader IDbCommand.ExecuteReader(CommandBehavior behavior) {
  173. return (DbDataReader)ExecuteDbDataReader(behavior);
  174. }
  175. public Task<int> ExecuteNonQueryAsync() {
  176. return ExecuteNonQueryAsync(CancellationToken.None);
  177. }
  178. public virtual Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken) {
  179. if (cancellationToken.IsCancellationRequested) {
  180. return ADP.CreatedTaskWithCancellation<int>();
  181. }
  182. else {
  183. CancellationTokenRegistration registration = new CancellationTokenRegistration();
  184. if (cancellationToken.CanBeCanceled) {
  185. registration = cancellationToken.Register(CancelIgnoreFailure);
  186. }
  187. try {
  188. return Task.FromResult<int>(ExecuteNonQuery());
  189. }
  190. catch (Exception e) {
  191. registration.Dispose();
  192. return ADP.CreatedTaskWithException<int>(e);
  193. }
  194. }
  195. }
  196. public Task<DbDataReader> ExecuteReaderAsync() {
  197. return ExecuteReaderAsync(CommandBehavior.Default, CancellationToken.None);
  198. }
  199. public Task<DbDataReader> ExecuteReaderAsync(CancellationToken cancellationToken) {
  200. return ExecuteReaderAsync(CommandBehavior.Default, cancellationToken);
  201. }
  202. public Task<DbDataReader> ExecuteReaderAsync(CommandBehavior behavior) {
  203. return ExecuteReaderAsync(behavior, CancellationToken.None);
  204. }
  205. public Task<DbDataReader> ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) {
  206. return ExecuteDbDataReaderAsync(behavior, cancellationToken);
  207. }
  208. protected virtual Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) {
  209. if (cancellationToken.IsCancellationRequested) {
  210. return ADP.CreatedTaskWithCancellation<DbDataReader>();
  211. }
  212. else {
  213. CancellationTokenRegistration registration = new CancellationTokenRegistration();
  214. if (cancellationToken.CanBeCanceled) {
  215. registration = cancellationToken.Register(CancelIgnoreFailure);
  216. }
  217. try {
  218. return Task.FromResult<DbDataReader>(ExecuteReader(behavior));
  219. }
  220. catch (Exception e) {
  221. registration.Dispose();
  222. return ADP.CreatedTaskWithException<DbDataReader>(e);
  223. }
  224. }
  225. }
  226. public Task<object> ExecuteScalarAsync() {
  227. return ExecuteScalarAsync(CancellationToken.None);
  228. }
  229. public virtual Task<object> ExecuteScalarAsync(CancellationToken cancellationToken) {
  230. if (cancellationToken.IsCancellationRequested) {
  231. return ADP.CreatedTaskWithCancellation<object>();
  232. }
  233. else {
  234. CancellationTokenRegistration registration = new CancellationTokenRegistration();
  235. if (cancellationToken.CanBeCanceled) {
  236. registration = cancellationToken.Register(CancelIgnoreFailure);
  237. }
  238. try {
  239. return Task.FromResult<object>(ExecuteScalar());
  240. }
  241. catch (Exception e) {
  242. registration.Dispose();
  243. return ADP.CreatedTaskWithException<object>(e);
  244. }
  245. }
  246. }
  247. abstract public object ExecuteScalar();
  248. abstract public void Prepare();
  249. }
  250. }