Beispiel #1
0
        private void BindParameter(IntPtr stmt, int index, object value)
        {
            if (value == null)
            {
                SQLiteNative.BindNull(stmt, index);
                return;
            }
            if (value is Int32)
            {
                SQLiteNative.BindInt(stmt, index, (int)value);
                return;
            }
            var stringValue = value as string;

            if (stringValue != null)
            {
                SQLiteNative.BindText(stmt, index, stringValue, -1, NegativePointer);
                return;
            }
            if (value is bool)
            {
                SQLiteNative.BindInt(stmt, index, (bool)value ? 1 : 0);
                return;
            }
            if (value is Byte || value is UInt16 || value is SByte || value is Int16)
            {
                SQLiteNative.BindInt(stmt, index, Convert.ToInt32(value));
                return;
            }
            if (value is UInt32 || value is Int64)
            {
                SQLiteNative.BindInt64(stmt, index, Convert.ToInt64(value));
                return;
            }
            if (value is Single || value is Double || value is Decimal)
            {
                SQLiteNative.BindDouble(stmt, index, Convert.ToDouble(value));
                return;
            }
            if (value is DateTime)
            {
                var dateTimeValue = (DateTime)value;
                if (_connection.DateTimeAsTicks)
                {
                    SQLiteNative.BindDouble(stmt, index, SQLiteParameter.ToJulianDay(dateTimeValue));
                }
                else
                {
                    SQLiteNative.BindText(stmt, index, dateTimeValue.ToString("yyyy-MM-dd HH:mm:ss"), -1, NegativePointer);
                }
                return;
            }
            var bytesValue = value as byte[];

            if (bytesValue != null)
            {
                SQLiteNative.BindBlob(stmt, index, bytesValue, bytesValue.Length, NegativePointer);
                return;
            }
            try
            {
                SQLiteNative.BindInt(stmt, index, (int)value);
            }
            catch
            {
                throw new NotSupportedException("Cannot store type: " + value.GetType());
            }
        }