Ejemplo n.º 1
0
        /// <summary>
        /// Perform the bind operation for an individual parameter
        /// </summary>
        /// <param name="index">The index of the parameter to bind</param>
        /// <param name="param">The parameter we're binding</param>
        private void BindParameter(int index, SqliteParameter param)
        {
            if (param == null)
            {
                throw new SqliteException((int)SQLiteErrorCode.Error, "Insufficient parameters supplied to the command");
            }

            object obj     = param.Value;
            DbType objType = param.DbType;

            if (Convert.IsDBNull(obj) || obj == null)
            {
                _sql.Bind_Null(this, index);
                return;
            }

            if (objType == DbType.Object)
            {
                objType = SqliteConvert.TypeToDbType(obj.GetType());
            }

            switch (objType)
            {
            case DbType.Date:
            case DbType.Time:
            case DbType.DateTime:
                _sql.Bind_DateTime(this, index, Convert.ToDateTime(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.UInt32:
            case DbType.Int64:
            case DbType.UInt64:
                _sql.Bind_Int64(this, index, Convert.ToInt64(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Boolean:
            case DbType.Int16:
            case DbType.Int32:
            case DbType.UInt16:
            case DbType.SByte:
            case DbType.Byte:
                _sql.Bind_Int32(this, index, Convert.ToInt32(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Single:
            case DbType.Double:
            case DbType.Currency:
                //case DbType.Decimal: // Dont store decimal as double ... loses precision
                _sql.Bind_Double(this, index, Convert.ToDouble(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Binary:
                _sql.Bind_Blob(this, index, (byte[])obj);
                break;

            case DbType.Guid:
                if (_command.Connection._binaryGuid == true)
                {
                    _sql.Bind_Blob(this, index, ((Guid)obj).ToByteArray());
                }
                else
                {
                    _sql.Bind_Text(this, index, obj.ToString());
                }

                break;

            case DbType.Decimal: // Dont store decimal as double ... loses precision
                _sql.Bind_Text(this, index, Convert.ToDecimal(obj, CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture));
                break;

            default:
                _sql.Bind_Text(this, index, obj.ToString());
                break;
            }
        }