SqlParameter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. //
  2. // System.Data.SqlClient.SqlParameter.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. // Diego Caravana ([email protected])
  9. //
  10. // (C) Ximian, Inc. 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. using Mono.Data.Tds;
  14. using Mono.Data.Tds.Protocol;
  15. using System;
  16. using System.ComponentModel;
  17. using System.Data;
  18. using System.Data.Common;
  19. using System.Runtime.InteropServices;
  20. using System.Text;
  21. namespace System.Data.SqlClient {
  22. [TypeConverterAttribute ("System.Data.SqlClient.SqlParameterConverter, "+ Consts.AssemblySystem_Data)]
  23. public sealed class SqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  24. {
  25. #region Fields
  26. TdsMetaParameter metaParameter;
  27. SqlParameterCollection container = null;
  28. DbType dbType;
  29. ParameterDirection direction = ParameterDirection.Input;
  30. bool isNullable;
  31. bool isSizeSet = false;
  32. bool isTypeSet = false;
  33. int offset;
  34. SqlDbType sqlDbType;
  35. string sourceColumn;
  36. DataRowVersion sourceVersion;
  37. #endregion // Fields
  38. #region Constructors
  39. public SqlParameter ()
  40. : this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  41. {
  42. }
  43. public SqlParameter (string parameterName, object value)
  44. {
  45. metaParameter = new TdsMetaParameter (parameterName, value);
  46. this.sourceVersion = DataRowVersion.Current;
  47. InferSqlType (value);
  48. }
  49. public SqlParameter (string parameterName, SqlDbType dbType)
  50. : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  51. {
  52. }
  53. public SqlParameter (string parameterName, SqlDbType dbType, int size)
  54. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  55. {
  56. }
  57. public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn)
  58. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  59. {
  60. }
  61. [EditorBrowsable (EditorBrowsableState.Advanced)]
  62. public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  63. {
  64. metaParameter = new TdsMetaParameter (parameterName, size, isNullable, precision, scale, value);
  65. SqlDbType = dbType;
  66. Direction = direction;
  67. SourceColumn = sourceColumn;
  68. SourceVersion = sourceVersion;
  69. }
  70. // This constructor is used internally to construct a
  71. // SqlParameter. The value array comes from sp_procedure_params_rowset.
  72. // This is in SqlCommand.DeriveParameters.
  73. internal SqlParameter (object[] dbValues)
  74. {
  75. Precision = 0;
  76. Scale = 0;
  77. Direction = ParameterDirection.Input;
  78. ParameterName = (string) dbValues[3];
  79. switch ((short) dbValues[5]) {
  80. case 1:
  81. Direction = ParameterDirection.Input;
  82. break;
  83. case 2:
  84. Direction = ParameterDirection.Output;
  85. break;
  86. case 3:
  87. Direction = ParameterDirection.InputOutput;
  88. break;
  89. case 4:
  90. Direction = ParameterDirection.ReturnValue;
  91. break;
  92. }
  93. IsNullable = (bool) dbValues[8];
  94. if (dbValues[12] != null)
  95. Precision = (byte) ((short) dbValues[12]);
  96. if (dbValues[13] != null)
  97. Scale = (byte) ((short) dbValues[13]);
  98. SetDbTypeName ((string) dbValues[16]);
  99. }
  100. #endregion // Constructors
  101. #region Properties
  102. // Used to ensure that only one collection can contain this
  103. // parameter
  104. internal SqlParameterCollection Container {
  105. get { return container; }
  106. set { container = value; }
  107. }
  108. [Browsable (false)]
  109. [DataCategory ("Data")]
  110. [DataSysDescription ("The parameter generic type.")]
  111. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  112. [RefreshProperties (RefreshProperties.All)]
  113. public DbType DbType {
  114. get { return dbType; }
  115. set {
  116. SetDbType (value);
  117. isTypeSet = true;
  118. }
  119. }
  120. [DataCategory ("Data")]
  121. [DataSysDescription ("Input, output, or bidirectional parameter.")]
  122. [DefaultValue (ParameterDirection.Input)]
  123. public ParameterDirection Direction {
  124. get { return direction; }
  125. set {
  126. direction = value;
  127. switch( direction ) {
  128. case ParameterDirection.Output:
  129. MetaParameter.Direction = TdsParameterDirection.Output;
  130. break;
  131. case ParameterDirection.InputOutput:
  132. MetaParameter.Direction = TdsParameterDirection.InputOutput;
  133. break;
  134. case ParameterDirection.ReturnValue:
  135. MetaParameter.Direction = TdsParameterDirection.ReturnValue;
  136. break;
  137. }
  138. }
  139. }
  140. internal TdsMetaParameter MetaParameter {
  141. get { return metaParameter; }
  142. }
  143. string IDataParameter.ParameterName {
  144. get { return metaParameter.ParameterName; }
  145. set { metaParameter.ParameterName = value; }
  146. }
  147. [Browsable (false)]
  148. [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
  149. [DefaultValue (false)]
  150. [DesignOnly (true)]
  151. [EditorBrowsable (EditorBrowsableState.Advanced)]
  152. public bool IsNullable {
  153. get { return metaParameter.IsNullable; }
  154. set { metaParameter.IsNullable = value; }
  155. }
  156. [Browsable (false)]
  157. [DataCategory ("Data")]
  158. [DataSysDescription ("Offset in variable length data types.")]
  159. [DefaultValue (0)]
  160. public int Offset {
  161. get { return offset; }
  162. set { offset = value; }
  163. }
  164. [DataSysDescription ("Name of the parameter, like '@p1'")]
  165. [DefaultValue ("")]
  166. public string ParameterName {
  167. get { return metaParameter.ParameterName; }
  168. set { metaParameter.ParameterName = value; }
  169. }
  170. [DataCategory ("Data")]
  171. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  172. [DefaultValue (0)]
  173. public byte Precision {
  174. get { return metaParameter.Precision; }
  175. set { metaParameter.Precision = value; }
  176. }
  177. [DataCategory ("Data")]
  178. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  179. [DefaultValue (0)]
  180. public byte Scale {
  181. get { return metaParameter.Scale; }
  182. set { metaParameter.Scale = value; }
  183. }
  184. [DataCategory ("Data")]
  185. [DataSysDescription ("Size of variable length datatypes (strings & arrays).")]
  186. [DefaultValue (0)]
  187. public int Size {
  188. get { return metaParameter.Size; }
  189. set { metaParameter.Size = value; }
  190. }
  191. [DataCategory ("Data")]
  192. [DataSysDescription ("When used by a DataAdapter.Update, the source column name that is used to find the DataSetColumn name in the ColumnMappings. This is to copy a value between the parameter and a datarow.")]
  193. [DefaultValue ("")]
  194. public string SourceColumn {
  195. get { return sourceColumn; }
  196. set { sourceColumn = value; }
  197. }
  198. [DataCategory ("Data")]
  199. [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
  200. [DefaultValue (DataRowVersion.Current)]
  201. public DataRowVersion SourceVersion {
  202. get { return sourceVersion; }
  203. set { sourceVersion = value; }
  204. }
  205. [DataCategory ("Data")]
  206. [DataSysDescription ("The parameter native type.")]
  207. [DefaultValue (SqlDbType.NVarChar)]
  208. [RefreshProperties (RefreshProperties.All)]
  209. public SqlDbType SqlDbType {
  210. get { return sqlDbType; }
  211. set {
  212. SetSqlDbType (value);
  213. isTypeSet = true;
  214. }
  215. }
  216. [DataCategory ("Data")]
  217. [DataSysDescription ("Value of the parameter.")]
  218. [DefaultValue (null)]
  219. [TypeConverterAttribute ("System.ComponentModel.StringConverter, "+ Consts.AssemblySystem)]
  220. public object Value {
  221. get { return metaParameter.Value; }
  222. set {
  223. if (!isTypeSet)
  224. InferSqlType (value);
  225. metaParameter.Value = value;
  226. }
  227. }
  228. #endregion // Properties
  229. #region Methods
  230. object ICloneable.Clone ()
  231. {
  232. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  233. }
  234. // If the value is set without the DbType/SqlDbType being set, then we
  235. // infer type information.
  236. private void InferSqlType (object value)
  237. {
  238. Type type = value.GetType ();
  239. string exception = String.Format ("The parameter data type of {0} is invalid.", type.Name);
  240. switch (type.FullName) {
  241. case "System.Int64":
  242. SetSqlDbType (SqlDbType.BigInt);
  243. break;
  244. case "System.Boolean":
  245. SetSqlDbType (SqlDbType.Bit);
  246. break;
  247. case "System.String":
  248. SetSqlDbType (SqlDbType.NVarChar);
  249. break;
  250. case "System.DateTime":
  251. SetSqlDbType (SqlDbType.DateTime);
  252. break;
  253. case "System.Decimal":
  254. SetSqlDbType (SqlDbType.Decimal);
  255. break;
  256. case "System.Double":
  257. SetSqlDbType (SqlDbType.Float);
  258. break;
  259. case "System.Byte[]":
  260. SetSqlDbType (SqlDbType.VarBinary);
  261. break;
  262. case "System.Byte":
  263. SetSqlDbType (SqlDbType.TinyInt);
  264. break;
  265. case "System.Int32":
  266. SetSqlDbType (SqlDbType.Int);
  267. break;
  268. case "System.Single":
  269. SetSqlDbType (SqlDbType.Real);
  270. break;
  271. case "System.Int16":
  272. SetSqlDbType (SqlDbType.SmallInt);
  273. break;
  274. case "System.Guid":
  275. SetSqlDbType (SqlDbType.UniqueIdentifier);
  276. break;
  277. case "System.Object":
  278. SetSqlDbType (SqlDbType.Variant);
  279. break;
  280. default:
  281. throw new ArgumentException (exception);
  282. }
  283. }
  284. // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
  285. // string representation of the type name. If the DbType is not convertible
  286. // to an SqlDbType, throw an exception.
  287. private void SetDbType (DbType type)
  288. {
  289. string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
  290. switch (type) {
  291. case DbType.AnsiString:
  292. MetaParameter.TypeName = "varchar";
  293. sqlDbType = SqlDbType.VarChar;
  294. break;
  295. case DbType.AnsiStringFixedLength:
  296. MetaParameter.TypeName = "char";
  297. sqlDbType = SqlDbType.Char;
  298. break;
  299. case DbType.Binary:
  300. MetaParameter.TypeName = "varbinary";
  301. sqlDbType = SqlDbType.VarBinary;
  302. break;
  303. case DbType.Boolean:
  304. MetaParameter.TypeName = "bit";
  305. sqlDbType = SqlDbType.Bit;
  306. break;
  307. case DbType.Byte:
  308. MetaParameter.TypeName = "tinyint";
  309. sqlDbType = SqlDbType.TinyInt;
  310. break;
  311. case DbType.Currency:
  312. sqlDbType = SqlDbType.Money;
  313. MetaParameter.TypeName = "money";
  314. break;
  315. case DbType.Date:
  316. case DbType.DateTime:
  317. MetaParameter.TypeName = "datetime";
  318. sqlDbType = SqlDbType.DateTime;
  319. break;
  320. case DbType.Decimal:
  321. MetaParameter.TypeName = "decimal";
  322. sqlDbType = SqlDbType.Decimal;
  323. break;
  324. case DbType.Double:
  325. MetaParameter.TypeName = "float";
  326. sqlDbType = SqlDbType.Float;
  327. break;
  328. case DbType.Guid:
  329. MetaParameter.TypeName = "uniqueidentifier";
  330. sqlDbType = SqlDbType.UniqueIdentifier;
  331. break;
  332. case DbType.Int16:
  333. MetaParameter.TypeName = "smallint";
  334. sqlDbType = SqlDbType.SmallInt;
  335. break;
  336. case DbType.Int32:
  337. MetaParameter.TypeName = "int";
  338. sqlDbType = SqlDbType.Int;
  339. break;
  340. case DbType.Int64:
  341. MetaParameter.TypeName = "bigint";
  342. sqlDbType = SqlDbType.BigInt;
  343. break;
  344. case DbType.Object:
  345. MetaParameter.TypeName = "sql_variant";
  346. sqlDbType = SqlDbType.Variant;
  347. break;
  348. case DbType.Single:
  349. MetaParameter.TypeName = "real";
  350. sqlDbType = SqlDbType.Real;
  351. break;
  352. case DbType.String:
  353. MetaParameter.TypeName = "nvarchar";
  354. sqlDbType = SqlDbType.NVarChar;
  355. break;
  356. case DbType.StringFixedLength:
  357. MetaParameter.TypeName = "nchar";
  358. sqlDbType = SqlDbType.NChar;
  359. break;
  360. case DbType.Time:
  361. MetaParameter.TypeName = "datetime";
  362. sqlDbType = SqlDbType.DateTime;
  363. break;
  364. default:
  365. throw new ArgumentException (exception);
  366. }
  367. dbType = type;
  368. }
  369. // Used by internal constructor which has a SQL Server typename
  370. private void SetDbTypeName (string dbTypeName)
  371. {
  372. switch (dbTypeName.ToLower ()) {
  373. case "bigint":
  374. SqlDbType = SqlDbType.BigInt;
  375. break;
  376. case "binary":
  377. SqlDbType = SqlDbType.Binary;
  378. break;
  379. case "bit":
  380. SqlDbType = SqlDbType.Bit;
  381. break;
  382. case "char":
  383. SqlDbType = SqlDbType.Char;
  384. break;
  385. case "datetime":
  386. SqlDbType = SqlDbType.DateTime;
  387. break;
  388. case "decimal":
  389. SqlDbType = SqlDbType.Decimal;
  390. break;
  391. case "float":
  392. SqlDbType = SqlDbType.Float;
  393. break;
  394. case "image":
  395. SqlDbType = SqlDbType.Image;
  396. break;
  397. case "int":
  398. SqlDbType = SqlDbType.Int;
  399. break;
  400. case "money":
  401. SqlDbType = SqlDbType.Money;
  402. break;
  403. case "nchar":
  404. SqlDbType = SqlDbType.NChar;
  405. break;
  406. case "ntext":
  407. SqlDbType = SqlDbType.NText;
  408. break;
  409. case "nvarchar":
  410. SqlDbType = SqlDbType.NVarChar;
  411. break;
  412. case "real":
  413. SqlDbType = SqlDbType.Real;
  414. break;
  415. case "smalldatetime":
  416. SqlDbType = SqlDbType.SmallDateTime;
  417. break;
  418. case "smallint":
  419. SqlDbType = SqlDbType.SmallInt;
  420. break;
  421. case "smallmoney":
  422. SqlDbType = SqlDbType.SmallMoney;
  423. break;
  424. case "text":
  425. SqlDbType = SqlDbType.Text;
  426. break;
  427. case "timestamp":
  428. SqlDbType = SqlDbType.Timestamp;
  429. break;
  430. case "tinyint":
  431. SqlDbType = SqlDbType.TinyInt;
  432. break;
  433. case "uniqueidentifier":
  434. SqlDbType = SqlDbType.UniqueIdentifier;
  435. break;
  436. case "varbinary":
  437. SqlDbType = SqlDbType.VarBinary;
  438. break;
  439. case "varchar":
  440. SqlDbType = SqlDbType.VarChar;
  441. break;
  442. default:
  443. SqlDbType = SqlDbType.Variant;
  444. break;
  445. }
  446. }
  447. // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
  448. // string representation of the type name. If the SqlDbType is not convertible
  449. // to a DbType, throw an exception.
  450. private void SetSqlDbType (SqlDbType type)
  451. {
  452. string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
  453. switch (type) {
  454. case SqlDbType.BigInt:
  455. MetaParameter.TypeName = "bigint";
  456. dbType = DbType.Int64;
  457. break;
  458. case SqlDbType.Binary:
  459. MetaParameter.TypeName = "binary";
  460. dbType = DbType.Binary;
  461. break;
  462. case SqlDbType.Timestamp:
  463. MetaParameter.TypeName = "timestamp";
  464. dbType = DbType.Binary;
  465. break;
  466. case SqlDbType.VarBinary:
  467. MetaParameter.TypeName = "varbinary";
  468. dbType = DbType.Binary;
  469. break;
  470. case SqlDbType.Bit:
  471. MetaParameter.TypeName = "bit";
  472. dbType = DbType.Boolean;
  473. break;
  474. case SqlDbType.Char:
  475. MetaParameter.TypeName = "char";
  476. dbType = DbType.AnsiStringFixedLength;
  477. break;
  478. case SqlDbType.DateTime:
  479. MetaParameter.TypeName = "datetime";
  480. dbType = DbType.DateTime;
  481. break;
  482. case SqlDbType.SmallDateTime:
  483. MetaParameter.TypeName = "smalldatetime";
  484. dbType = DbType.DateTime;
  485. break;
  486. case SqlDbType.Decimal:
  487. MetaParameter.TypeName = "decimal";
  488. dbType = DbType.Decimal;
  489. break;
  490. case SqlDbType.Float:
  491. MetaParameter.TypeName = "float";
  492. dbType = DbType.Double;
  493. break;
  494. case SqlDbType.Image:
  495. MetaParameter.TypeName = "image";
  496. dbType = DbType.Binary;
  497. break;
  498. case SqlDbType.Int:
  499. MetaParameter.TypeName = "int";
  500. dbType = DbType.Int32;
  501. break;
  502. case SqlDbType.Money:
  503. MetaParameter.TypeName = "money";
  504. dbType = DbType.Currency;
  505. break;
  506. case SqlDbType.SmallMoney:
  507. MetaParameter.TypeName = "smallmoney";
  508. dbType = DbType.Currency;
  509. break;
  510. case SqlDbType.NChar:
  511. MetaParameter.TypeName = "nchar";
  512. dbType = DbType.StringFixedLength;
  513. break;
  514. case SqlDbType.NText:
  515. MetaParameter.TypeName = "ntext";
  516. dbType = DbType.String;
  517. break;
  518. case SqlDbType.NVarChar:
  519. MetaParameter.TypeName = "nvarchar";
  520. dbType = DbType.String;
  521. break;
  522. case SqlDbType.Real:
  523. MetaParameter.TypeName = "real";
  524. dbType = DbType.Single;
  525. break;
  526. case SqlDbType.SmallInt:
  527. MetaParameter.TypeName = "smallint";
  528. dbType = DbType.Int16;
  529. break;
  530. case SqlDbType.Text:
  531. MetaParameter.TypeName = "text";
  532. dbType = DbType.AnsiString;
  533. break;
  534. case SqlDbType.VarChar:
  535. MetaParameter.TypeName = "varchar";
  536. dbType = DbType.AnsiString;
  537. break;
  538. case SqlDbType.TinyInt:
  539. MetaParameter.TypeName = "tinyint";
  540. dbType = DbType.Byte;
  541. break;
  542. case SqlDbType.UniqueIdentifier:
  543. MetaParameter.TypeName = "uniqueidentifier";
  544. dbType = DbType.Guid;
  545. break;
  546. case SqlDbType.Variant:
  547. MetaParameter.TypeName = "sql_variant";
  548. dbType = DbType.Object;
  549. break;
  550. default:
  551. throw new ArgumentException (exception);
  552. }
  553. sqlDbType = type;
  554. }
  555. public override string ToString()
  556. {
  557. return ParameterName;
  558. }
  559. #endregion // Methods
  560. }
  561. }