2 // System.Data.OleDb.OleDbParameter
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System
.Data
.Common
;
38 using System
.ComponentModel
;
40 namespace System
.Data
.OleDb
42 [TypeConverterAttribute (typeof (OleDbParameterConverter
))]
43 public sealed class OleDbParameter
: MarshalByRefObject
, IDbDataParameter
, IDataParameter
, ICloneable
53 DataRowVersion sourceVersion
;
55 ParameterDirection direction
;
58 OleDbParameterCollection container
= null;
66 public OleDbParameter ()
74 sourceColumn
= String
.Empty
;
75 gdaParameter
= IntPtr
.Zero
;
78 public OleDbParameter (string name
, object value)
83 OleDbType
= GetOleDbType (value);
86 public OleDbParameter (string name
, OleDbType dataType
)
93 public OleDbParameter (string name
, OleDbType dataType
, int size
)
94 : this (name
, dataType
)
99 public OleDbParameter (string name
, OleDbType dataType
, int size
, string srcColumn
)
100 : this (name
, dataType
, size
)
102 this.sourceColumn
= srcColumn
;
105 [EditorBrowsableAttribute (EditorBrowsableState
.Advanced
)]
106 public OleDbParameter(string name
, OleDbType dataType
, int size
, ParameterDirection direction
, bool isNullable
, byte precision
, byte scale
, string srcColumn
, DataRowVersion srcVersion
, object value)
107 : this (name
, dataType
, size
, srcColumn
)
109 this.direction
= direction
;
110 this.isNullable
= isNullable
;
111 this.precision
= precision
;
113 this.sourceVersion
= srcVersion
;
121 [BrowsableAttribute (false)]
122 [DataSysDescriptionAttribute ("The parameter generic type")]
123 [RefreshPropertiesAttribute (RefreshProperties
.All
)]
124 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility
.Hidden
)]
125 [DataCategory ("DataCategory_Data")]
126 public DbType DbType
{
127 get { return dbType; }
130 oleDbType
= DbTypeToOleDbType (value);
134 [DataSysDescriptionAttribute ("Input, output, or bidirectional parameter")] [DefaultValue (ParameterDirection
.Input
)]
135 [DataCategory ("DataCategory_Data")]
136 public ParameterDirection Direction
{
137 get { return direction; }
138 set { direction = value; }
141 [BrowsableAttribute (false)]
142 [DataSysDescriptionAttribute ("A design-time property used for strongly typed code generation")]
143 [DesignOnlyAttribute (true)]
144 [EditorBrowsableAttribute (EditorBrowsableState
.Advanced
)]
145 [DefaultValue (false)]
146 public bool IsNullable
{
147 get { return isNullable; }
148 set { isNullable = value; }
151 [DefaultValue (OleDbType
.VarWChar
)]
152 [DataSysDescriptionAttribute ("The parameter native type")]
153 [RefreshPropertiesAttribute (RefreshProperties
.All
)]
154 [DataCategory ("DataCategory_Data")]
155 public OleDbType OleDbType
{
156 get { return oleDbType; }
159 dbType
= OleDbTypeToDbType (value);
164 [DataSysDescriptionAttribute ("Name of the parameter")]
165 public string ParameterName
{
167 set { name = value; }
171 [DataSysDescriptionAttribute ("For decimal, numeric, varnumeric DBTypes")]
172 [DataCategory ("DataCategory_Data")]
173 public byte Precision
{
174 get { return precision; }
175 set { precision = value; }
179 [DataSysDescriptionAttribute ("For decimal, numeric, varnumeric DBTypes")]
180 [DataCategory ("DataCategory_Data")]
182 get { return scale; }
183 set { scale = value; }
188 [DataSysDescriptionAttribute ("Size of variable length data types (string & arrays)")]
189 [DataCategory ("DataCategory_Data")]
192 set { size = value; }
196 [DataSysDescriptionAttribute ("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")]
197 [DataCategory ("DataCategory_Data")]
198 public string SourceColumn
{
199 get { return sourceColumn; }
200 set { sourceColumn = value; }
203 [DefaultValue (DataRowVersion
.Current
)]
204 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update(UpdateCommand only), the version of the DataRow value that is used to update the data source")]
205 [DataCategory ("DataCategory_Data")]
206 public DataRowVersion SourceVersion
{
207 get { return sourceVersion; }
208 set { sourceVersion = value; }
211 [DefaultValue (null)]
212 [DataSysDescriptionAttribute ("value of the parameter")]
213 [TypeConverter (typeof (StringConverter
))]
214 [DataCategory ("DataCategory_Data")]
215 public object Value
{
216 get { return value; }
217 set { this.value = value; }
220 // Used to ensure that only one collection can contain this
222 internal OleDbParameterCollection Container
{
223 get { return container; }
224 set { container = value; }
228 #endregion // Properties
230 #region Internal Properties
232 internal IntPtr GdaParameter
{
233 get { return gdaParameter; }
236 #endregion // Internal Properties
241 object ICloneable
.Clone ()
243 throw new NotImplementedException ();
246 public override string ToString ()
248 return ParameterName
;
251 private OleDbType
DbTypeToOleDbType (DbType dbType
)
254 case DbType
.AnsiString
:
255 return OleDbType
.VarChar
;
256 case DbType
.AnsiStringFixedLength
:
257 return OleDbType
.Char
;
259 return OleDbType
.Binary
;
260 case DbType
.Boolean
:
261 return OleDbType
.Boolean
;
263 return OleDbType
.UnsignedTinyInt
;
264 case DbType
.Currency
:
265 return OleDbType
.Currency
;
267 return OleDbType
.Date
;
268 case DbType
.DateTime
:
269 throw new NotImplementedException ();
270 case DbType
.Decimal
:
271 return OleDbType
.Decimal
;
273 return OleDbType
.Double
;
275 return OleDbType
.Guid
;
277 return OleDbType
.SmallInt
;
279 return OleDbType
.Integer
;
281 return OleDbType
.BigInt
;
283 return OleDbType
.Variant
;
285 return OleDbType
.TinyInt
;
287 return OleDbType
.Single
;
289 return OleDbType
.WChar
;
290 case DbType
.StringFixedLength
:
291 return OleDbType
.VarWChar
;
293 throw new NotImplementedException ();
295 return OleDbType
.UnsignedSmallInt
;
297 return OleDbType
.UnsignedInt
;
299 return OleDbType
.UnsignedBigInt
;
300 case DbType
.VarNumeric
:
301 return OleDbType
.VarNumeric
;
303 return OleDbType
.Variant
;
306 private DbType
OleDbTypeToDbType (OleDbType oleDbType
)
309 case OleDbType
.BigInt
:
311 case OleDbType
.Binary
:
312 return DbType
.Binary
;
313 case OleDbType
.Boolean
:
314 return DbType
.Boolean
;
315 case OleDbType
.BSTR
:
316 return DbType
.AnsiString
;
317 case OleDbType
.Char
:
318 return DbType
.AnsiStringFixedLength
;
319 case OleDbType
.Currency
:
320 return DbType
.Currency
;
321 case OleDbType
.Date
:
322 return DbType
.DateTime
;
323 case OleDbType
.DBDate
:
324 return DbType
.DateTime
;
325 case OleDbType
.DBTime
:
326 throw new NotImplementedException ();
327 case OleDbType
.DBTimeStamp
:
328 return DbType
.DateTime
;
329 case OleDbType
.Decimal
:
330 return DbType
.Decimal
;
331 case OleDbType
.Double
:
332 return DbType
.Double
;
333 case OleDbType
.Empty
:
334 throw new NotImplementedException ();
335 case OleDbType
.Error
:
336 throw new NotImplementedException ();
337 case OleDbType
.Filetime
:
338 return DbType
.DateTime
;
339 case OleDbType
.Guid
:
341 case OleDbType
.IDispatch
:
342 return DbType
.Object
;
343 case OleDbType
.Integer
:
345 case OleDbType
.IUnknown
:
346 return DbType
.Object
;
347 case OleDbType
.LongVarBinary
:
348 return DbType
.Binary
;
349 case OleDbType
.LongVarChar
:
350 return DbType
.AnsiString
;
351 case OleDbType
.LongVarWChar
:
352 return DbType
.String
;
353 case OleDbType
.Numeric
:
354 return DbType
.Decimal
;
355 case OleDbType
.PropVariant
:
356 return DbType
.Object
;
357 case OleDbType
.Single
:
358 return DbType
.Single
;
359 case OleDbType
.SmallInt
:
361 case OleDbType
.TinyInt
:
363 case OleDbType
.UnsignedBigInt
:
364 return DbType
.UInt64
;
365 case OleDbType
.UnsignedInt
:
366 return DbType
.UInt32
;
367 case OleDbType
.UnsignedSmallInt
:
368 return DbType
.UInt16
;
369 case OleDbType
.UnsignedTinyInt
:
371 case OleDbType
.VarBinary
:
372 return DbType
.Binary
;
373 case OleDbType
.VarChar
:
374 return DbType
.AnsiString
;
375 case OleDbType
.Variant
:
376 return DbType
.Object
;
377 case OleDbType
.VarNumeric
:
378 return DbType
.VarNumeric
;
379 case OleDbType
.VarWChar
:
380 return DbType
.StringFixedLength
;
381 case OleDbType
.WChar
:
382 return DbType
.String
;
384 return DbType
.Object
;
387 private OleDbType
GetOleDbType (object value)
389 if (value is Guid
) return OleDbType
.Guid
;
390 if (value is TimeSpan
) return OleDbType
.DBTime
;
392 switch (Type
.GetTypeCode (value.GetType ())) {
393 case TypeCode
.Boolean
:
394 return OleDbType
.Boolean
;
396 if (value.GetType().IsArray
)
397 return OleDbType
.Binary
;
399 return OleDbType
.UnsignedTinyInt
;
401 return OleDbType
.Char
;
402 case TypeCode
.DateTime
:
403 return OleDbType
.Date
;
404 case TypeCode
.DBNull
:
405 return OleDbType
.Empty
;
406 case TypeCode
.Decimal
:
407 return OleDbType
.Decimal
;
408 case TypeCode
.Double
:
409 return OleDbType
.Double
;
410 case TypeCode
.Empty
:
411 return OleDbType
.Empty
;
412 case TypeCode
.Int16
:
413 return OleDbType
.SmallInt
;
414 case TypeCode
.Int32
:
415 return OleDbType
.Integer
;
416 case TypeCode
.Int64
:
417 return OleDbType
.BigInt
;
418 case TypeCode
.SByte
:
419 return OleDbType
.TinyInt
;
420 case TypeCode
.String
:
421 return OleDbType
.VarChar
;
422 case TypeCode
.Single
:
423 return OleDbType
.Single
;
424 case TypeCode
.UInt64
:
425 return OleDbType
.UnsignedBigInt
;
426 case TypeCode
.UInt32
:
427 return OleDbType
.UnsignedInt
;
428 case TypeCode
.UInt16
:
429 return OleDbType
.UnsignedSmallInt
;
430 case TypeCode
.Object
:
431 return OleDbType
.Variant
;
433 return OleDbType
.IUnknown
;