IBM.Data.DB2.DB2Parameter.Bind C# (CSharp) Method

Bind() private method

private Bind ( IntPtr hwndStmt, short paramNum ) : short
hwndStmt System.IntPtr
paramNum short
return short
		internal short Bind(IntPtr hwndStmt, short paramNum)
		{
			int inLength = requiredMemory;
			db2LastUsedDataType = db2DataType;
			short db2CType = DB2Constants.SQL_C_DEFAULT;
			if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
			{
				if(Convert.IsDBNull(Value))
				{
					inLength = DB2Constants.SQL_NULL_DATA;
					if((db2DataType == DB2Constants.SQL_UNKNOWN_TYPE) || 
						(db2DataType == DB2Constants.SQL_DECIMAL))
					{
						db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
						db2CType = DB2Constants.SQL_C_WCHAR;
					}
				}
			}
			if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
			{
				switch (db2DataType) 
				{
					case DB2Constants.SQL_WCHAR:
						string tmpString = Convert.ToString(Value);
						inLength =  tmpString.Length;
						if((Size > 0) && (inLength > Size))
							inLength = Size;
						Marshal.Copy(tmpString.ToCharArray(), 0, internalBuffer, inLength);
						inLength *= 2;
						db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
						db2CType = DB2Constants.SQL_C_WCHAR;
						if(inLength > 32000)
						{
							db2LastUsedDataType = DB2Constants.SQL_TYPE_BLOB;
						}
						break;
					case DB2Constants.SQL_VARBINARY:
						byte[] tmpBytes = (byte[])Value;
						inLength = tmpBytes.Length;
						if((Size > 0) && (inLength > Size))
							inLength = Size;
						Marshal.Copy(tmpBytes, 0, internalBuffer, inLength);
						db2CType = DB2Constants.SQL_TYPE_BINARY;
						break;
					case DB2Constants.SQL_BIT:
					case DB2Constants.SQL_UTINYINT:
					case DB2Constants.SQL_SMALLINT:
						Marshal.WriteInt16(internalBuffer, Convert.ToInt16(Value));
						db2CType = DB2Constants.SQL_C_SSHORT;
						break;
					case DB2Constants.SQL_INTEGER:
						Marshal.WriteInt32(internalBuffer, Convert.ToInt32(Value));
						db2CType = DB2Constants.SQL_C_SLONG;
						break;
					case DB2Constants.SQL_BIGINT:
						Marshal.WriteInt64(internalBuffer, Convert.ToInt64(Value));
						db2CType = DB2Constants.SQL_C_SBIGINT;
						break;
					case DB2Constants.SQL_REAL:
						Marshal.StructureToPtr((float)Convert.ToDouble(Value), internalBuffer, false);
						db2CType = DB2Constants.SQL_C_TYPE_REAL;
						break;
					case DB2Constants.SQL_DOUBLE:
						Marshal.StructureToPtr(Convert.ToDouble(Value), internalBuffer, false);
						db2CType = DB2Constants.SQL_C_DOUBLE;
						break;
					case DB2Constants.SQL_DECIMAL:
						byte[] tmpDecimalData = System.Text.Encoding.UTF8.GetBytes(
							Convert.ToDecimal(Value).ToString(System.Globalization.CultureInfo.InvariantCulture));
						inLength =  Math.Min(tmpDecimalData.Length, requiredMemory);
						Marshal.Copy(tmpDecimalData, 0, internalBuffer, inLength);
						db2LastUsedDataType = DB2Constants.SQL_VARCHAR;
						db2CType = DB2Constants.SQL_C_CHAR;
						break;
					case DB2Constants.SQL_TYPE_DATE:
						DateTime tmpDate = Convert.ToDateTime(Value);
						Marshal.WriteInt16(internalBuffer, 0,  (short)tmpDate.Year);
						Marshal.WriteInt16(internalBuffer, 2,  (short)tmpDate.Month);
						Marshal.WriteInt16(internalBuffer, 4,  (short)tmpDate.Day);
						db2CType = DB2Constants.SQL_C_TYPE_DATE;
						break;
					case DB2Constants.SQL_TYPE_TIMESTAMP:
						DateTime tmpDateTime = Convert.ToDateTime(Value);
						Marshal.WriteInt16(internalBuffer, 0,  (short)tmpDateTime.Year);
						Marshal.WriteInt16(internalBuffer, 2,  (short)tmpDateTime.Month);
						Marshal.WriteInt16(internalBuffer, 4,  (short)tmpDateTime.Day);
						Marshal.WriteInt16(internalBuffer, 6,  (short)tmpDateTime.Hour);
						Marshal.WriteInt16(internalBuffer, 8,  (short)tmpDateTime.Minute);
						Marshal.WriteInt16(internalBuffer, 10, (short)tmpDateTime.Second);
						Marshal.WriteInt32(internalBuffer, 12, (int)((tmpDateTime.Ticks % 10000000) * 100));
						db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;
						break;
					case DB2Constants.SQL_TYPE_TIME:
						TimeSpan tmpTime = (TimeSpan)Value;
						Marshal.WriteInt16(internalBuffer, 0,  (short)tmpTime.Hours);
						Marshal.WriteInt16(internalBuffer, 2,  (short)tmpTime.Minutes);
						Marshal.WriteInt16(internalBuffer, 4,  (short)tmpTime.Seconds);
						db2CType = DB2Constants.SQL_C_TYPE_TIME;
						break;
				}
			}
			else
			{
				switch (db2DataType) 
				{
					case DB2Constants.SQL_WCHAR:
						db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
						db2CType = DB2Constants.SQL_C_WCHAR;
						break;
					case DB2Constants.SQL_VARBINARY:
						db2CType = DB2Constants.SQL_TYPE_BINARY;
						break;
					case DB2Constants.SQL_BIT:
					case DB2Constants.SQL_UTINYINT:
					case DB2Constants.SQL_SMALLINT:
						db2CType = DB2Constants.SQL_C_SSHORT;
						break;
					case DB2Constants.SQL_INTEGER:
						db2CType = DB2Constants.SQL_C_SLONG;
						break;
					case DB2Constants.SQL_BIGINT:
						db2CType = DB2Constants.SQL_C_SBIGINT;
						break;
					case DB2Constants.SQL_REAL:
						db2CType = DB2Constants.SQL_C_TYPE_REAL;
						break;
					case DB2Constants.SQL_DOUBLE:
						db2CType = DB2Constants.SQL_C_DOUBLE;
						break;
					case DB2Constants.SQL_DECIMAL:
						db2LastUsedDataType = DB2Constants.SQL_VARCHAR;
						db2CType = DB2Constants.SQL_C_CHAR;
						break;
					case DB2Constants.SQL_TYPE_DATE:
						db2CType = DB2Constants.SQL_C_TYPE_DATE;
						break;
					case DB2Constants.SQL_TYPE_TIMESTAMP:
						db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;
						break;
					case DB2Constants.SQL_TYPE_TIME:
						db2CType = DB2Constants.SQL_C_TYPE_TIME;
						break;
				}
			}
			Marshal.WriteInt32(internalLengthBuffer, inLength);
			short sqlRet = DB2CLIWrapper.SQLBindParameter(hwndStmt, paramNum, db2Direction, 
				db2CType, db2LastUsedDataType, Size, Scale,
				internalBuffer, requiredMemory, internalLengthBuffer);

			return sqlRet;
		}
		#endregion

Usage Example

コード例 #1
0
 private void BindParams()
 {
     if (parameters.Count > 0)
     {
         statementParametersMemorySize = 0;
         int   offset = 0;
         short sqlRet;
         for (int i = 0; i < parameters.Count; i++)
         {
             if (commandType == CommandType.StoredProcedure)
             {
                 commandText = AddCallParam(commandText);
             }
             DB2Parameter param = parameters[i];
             param.CalculateRequiredmemory();
             statementParametersMemorySize += param.requiredMemory + 8;
             param.internalBuffer           = Marshal.AllocHGlobal(param.requiredMemory);
             offset += param.requiredMemory;
             param.internalLengthBuffer = Marshal.AllocHGlobal(4);
             Marshal.WriteInt32(param.internalLengthBuffer, param.requiredMemory);
             sqlRet = param.Bind(this.hwndStmt, (short)(i + 1));
             DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_STMT, hwndStmt, "Error binding parameter in DB2Command: ", db2Conn);
         }
         binded = true;
     }
 }