2 // System.Data.Odbc.OdbcParameter
5 // Brian Ritchie (brianlritchie@hotmail.com)
7 // Copyright (C) Brian Ritchie, 2002
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Data.Common;
36 using System.ComponentModel;
38 namespace System.Data.Odbc
40 [TypeConverterAttribute (typeof (OdbcParameterConverter))]
41 public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
51 DataRowVersion sourceVersion;
53 ParameterDirection direction;
56 OdbcParameterCollection container = null;
58 // Buffers for parameter value based on type. Currently I've only optimized
59 // for int parameters and everything else is just converted to a string.
60 private bool bufferIsSet;
68 public OdbcParameter ()
76 sourceColumn = String.Empty;
79 public OdbcParameter (string name, object value)
83 this.ParamValue = value;
85 if (value != null && !value.GetType ().IsValueType) {
86 Type type = value.GetType ();
88 size = type.GetElementType () == typeof (byte) ?
89 ((Array) value).Length : 0;
91 size = value.ToString ().Length;
97 public OdbcParameter (string name, OdbcType dataType)
104 public OdbcParameter (string name, OdbcType dataType, int size)
105 : this (name, dataType)
110 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
111 : this (name, dataType, size)
113 this.sourceColumn = srcColumn;
116 [EditorBrowsable (EditorBrowsableState.Advanced)]
117 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
118 : this (name, dataType, size, srcColumn)
120 this.direction = direction;
121 this.isNullable = isNullable;
122 this.precision = precision;
124 this.sourceVersion = srcVersion;
125 this.ParamValue = value;
132 // Used to ensure that only one collection can contain this
134 internal OdbcParameterCollection Container {
135 get { return container; }
136 set { container = value; }
139 [BrowsableAttribute (false)]
140 [OdbcDescriptionAttribute ("The parameter generic type")]
141 [RefreshPropertiesAttribute (RefreshProperties.All)]
142 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
143 [OdbcCategory ("Data")]
144 public DbType DbType {
145 get { return dbType; }
151 [OdbcCategory ("Data")]
152 [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
153 [DefaultValue (ParameterDirection.Input)]
154 public ParameterDirection Direction {
155 get { return direction; }
156 set { direction = value; }
159 [BrowsableAttribute (false)]
160 [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
161 [DesignOnlyAttribute (true)]
162 [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
163 [DefaultValue (false)]
164 public bool IsNullable {
165 get { return isNullable; }
\r
166 set { isNullable = value; }
\r
169 [DefaultValue (OdbcType.NChar)]
170 [OdbcDescriptionAttribute ("The parameter native type")]
171 [RefreshPropertiesAttribute (RefreshProperties.All)]
172 [OdbcCategory ("Data")]
173 public OdbcType OdbcType {
174 get { return odbcType; }
180 [OdbcDescription ("DataParameter_ParameterName")]
182 public string ParameterName {
184 set { name = value; }
187 [OdbcDescription ("DbDataParameter_Precision")]
188 [OdbcCategory ("DataCategory_Data")]
190 public byte Precision {
191 get { return precision; }
192 set { precision = value; }
195 [OdbcDescription ("DbDataParameter_Scale")]
196 [OdbcCategory ("DataCategory_Data")]
199 get { return scale; }
200 set { scale = value; }
203 [OdbcDescription ("DbDataParameter_Size")]
204 [OdbcCategory ("DataCategory_Data")]
208 set { size = value; }
211 [OdbcDescription ("DataParameter_SourceColumn")]
212 [OdbcCategory ("DataCategory_Data")]
214 public string SourceColumn {
215 get { return sourceColumn; }
216 set { sourceColumn = value; }
219 [OdbcDescription ("DataParameter_SourceVersion")]
220 [OdbcCategory ("DataCategory_Data")]
222 public DataRowVersion SourceVersion {
223 get { return sourceVersion; }
224 set { sourceVersion = value; }
227 [TypeConverter (typeof(StringConverter))]
228 [OdbcDescription ("DataParameter_Value")]
229 [OdbcCategory ("DataCategory_Data")]
230 [DefaultValue (null)]
231 public object Value {
236 this.ParamValue = value;
241 #endregion // Properties
245 internal void Bind(IntPtr hstmt, int ParamNum) {
\r
247 // Set up the buffer if we haven't done so yet
\r
251 // Convert System.Data.ParameterDirection into odbc enum
\r
252 OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
\r
253 // Bind parameter based on type
\r
254 if (odbcType == OdbcType.Int)
\r
255 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
\r
256 (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
\r
257 0, ref intbuf, 0, 0);
\r
259 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
\r
260 (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
\r
262 // Check for error condition
\r
263 if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
\r
264 throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
\r
267 private void setBuffer() {
\r
268 // Load buffer with new value
269 if (odbcType == OdbcType.Int)
270 intbuf = ParamValue == null ? new int () : (int) ParamValue;
272 string paramValueString = ParamValue.ToString();
\r
273 // Treat everything else as a string
\r
274 // Init string buffer
\r
275 if (ParamValue is String)
276 paramValueString = "\'"+paramValueString+"\'";
279 minSize = size > 20 ? size : 20;
280 if (buffer == null || buffer.Length < minSize)
281 buffer = new byte[minSize];
285 // Convert value into string and store into buffer
286 minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
287 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);
\r
289 bufferIsSet = true;
\r
293 object ICloneable.Clone ()
295 throw new NotImplementedException ();
298 public override string ToString ()
300 return ParameterName;