Exemple #1
0
        /// <summary>
        /// Turn a datatable into a table in the temporary database for the connection
        /// </summary>
        /// <param name="cnn">The connection to make the temporary table in</param>
        /// <param name="table">The table to write out</param>
        /// <param name="dest">The temporary table name to write to</param>
        private void DataTableToTable(SQLiteConnection cnn, DataTable table, string dest)
        {
            StringBuilder        sql     = new StringBuilder();
            SQLiteCommandBuilder builder = new SQLiteCommandBuilder();

            using (SQLiteCommand cmd = cnn.CreateCommand())
                using (DataTable source = new DataTable())
                {
                    sql.AppendFormat(CultureInfo.InvariantCulture, "CREATE TEMP TABLE {0} (", builder.QuoteIdentifier(dest));
                    string separator            = String.Empty;
                    SQLiteConnectionFlags flags = cnn.Flags;
                    foreach (DataColumn dc in table.Columns)
                    {
                        DbType dbtypeName = SQLiteConvert.TypeToDbType(dc.DataType);
                        string typeName   = SQLiteConvert.DbTypeToTypeName(cnn, dbtypeName, flags);

                        sql.AppendFormat(CultureInfo.InvariantCulture, "{2}{0} {1} COLLATE NOCASE", builder.QuoteIdentifier(dc.ColumnName), typeName, separator);
                        separator = ", ";
                    }
                    sql.Append(")");

                    cmd.CommandText = sql.ToString();
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = String.Format("SELECT * FROM TEMP.{0} WHERE 1=2", builder.QuoteIdentifier(dest));
                    using (SQLiteDataAdapter adp = new SQLiteDataAdapter(cmd))
                    {
                        builder.DataAdapter = adp;

                        adp.Fill(source);

                        foreach (DataRow row in table.Rows)
                        {
                            object[] arr = row.ItemArray;

                            source.Rows.Add(arr);
                        }
                        adp.Update(source);
                    }
                }
        }
        /// <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("Insufficient parameters supplied to the command");
            }

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

            if ((obj != null) && (objType == DbType.Object))
            {
                objType = SQLiteConvert.TypeToDbType(obj.GetType());
            }

            if ((_flags & SQLiteConnectionFlags.LogPreBind) == SQLiteConnectionFlags.LogPreBind)
            {
                IntPtr handle = _sqlite_stmt;

                SQLiteLog.LogMessage(String.Format(
                                         "Binding statement {0} paramter #{1} with database type {2} and raw value {{{3}}}...",
                                         handle, index, objType, obj));
            }

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

            CultureInfo invariantCultureInfo = CultureInfo.InvariantCulture;
            bool        invariantText        = ((_flags & SQLiteConnectionFlags.BindInvariantText)
                                                == SQLiteConnectionFlags.BindInvariantText);

            if ((_flags & SQLiteConnectionFlags.BindAllAsText) == SQLiteConnectionFlags.BindAllAsText)
            {
                if (obj is DateTime)
                {
                    _sql.Bind_DateTime(this, _flags, index, (DateTime)obj);
                }
                else
                {
                    _sql.Bind_Text(this, _flags, index, invariantText ?
                                   SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                                   obj.ToString());
                }

                return;
            }

            CultureInfo cultureInfo = CultureInfo.CurrentCulture;

            if ((_flags & SQLiteConnectionFlags.ConvertInvariantText) == SQLiteConnectionFlags.ConvertInvariantText)
            {
                cultureInfo = invariantCultureInfo;
            }

            switch (objType)
            {
            case DbType.Date:
            case DbType.Time:
            case DbType.DateTime:
                //
                // NOTE: The old method (commented below) does not honor the selected date format
                //       for the connection.
                // _sql.Bind_DateTime(this, index, Convert.ToDateTime(obj, cultureInfo));
                _sql.Bind_DateTime(this, _flags, index, (obj is string) ?
                                   _sql.ToDateTime((string)obj) : Convert.ToDateTime(obj, cultureInfo));
                break;

            case DbType.Boolean:
                _sql.Bind_Int32(this, _flags, index, SQLiteConvert.ToBoolean(obj, cultureInfo, true) ? 1 : 0);
                break;

            case DbType.SByte:
                _sql.Bind_Int32(this, _flags, index, Convert.ToSByte(obj, cultureInfo));
                break;

            case DbType.Int16:
                _sql.Bind_Int32(this, _flags, index, Convert.ToInt16(obj, cultureInfo));
                break;

            case DbType.Int32:
                _sql.Bind_Int32(this, _flags, index, Convert.ToInt32(obj, cultureInfo));
                break;

            case DbType.Int64:
                _sql.Bind_Int64(this, _flags, index, Convert.ToInt64(obj, cultureInfo));
                break;

            case DbType.Byte:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToByte(obj, cultureInfo));
                break;

            case DbType.UInt16:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt16(obj, cultureInfo));
                break;

            case DbType.UInt32:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt32(obj, cultureInfo));
                break;

            case DbType.UInt64:
                _sql.Bind_UInt64(this, _flags, index, Convert.ToUInt64(obj, cultureInfo));
                break;

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

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

            case DbType.Guid:
                if (_command.Connection._binaryGuid == true)
                {
                    _sql.Bind_Blob(this, _flags, index, ((Guid)obj).ToByteArray());
                }
                else
                {
                    _sql.Bind_Text(this, _flags, index, invariantText ?
                                   SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                                   obj.ToString());
                }
                break;

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

            default:
                _sql.Bind_Text(this, _flags, index, invariantText ?
                               SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                               obj.ToString());
                break;
            }
        }
Exemple #3
0
        private void BindParameter(int index, SQLiteParameter param)
        {
            bool                  flag;
            SQLiteBase            sQLiteBase;
            SQLiteConnectionFlags sQLiteConnectionFlag;
            int    num;
            string str;

            if (param == null)
            {
                throw new SQLiteException("Insufficient parameters supplied to the command");
            }
            if ((this._flags & SQLiteConnectionFlags.UseConnectionBindValueCallbacks) == SQLiteConnectionFlags.UseConnectionBindValueCallbacks)
            {
                this.InvokeBindValueCallback(index, param, out flag);
                if (flag)
                {
                    return;
                }
            }
            object value  = param.Value;
            DbType dbType = param.DbType;

            if (value != null && dbType == DbType.Object)
            {
                dbType = SQLiteConvert.TypeToDbType(value.GetType());
            }
            if (HelperMethods.LogPreBind(this._flags))
            {
                IntPtr      _sqliteStmt    = this._sqlite_stmt;
                CultureInfo currentCulture = CultureInfo.CurrentCulture;
                object[]    objArray       = new object[] { _sqliteStmt, index, dbType, value };
                SQLiteLog.LogMessage(HelperMethods.StringFormat(currentCulture, "Binding statement {0} paramter #{1} with database type {2} and raw value {{{3}}}...", objArray));
            }
            if (value == null || Convert.IsDBNull(value))
            {
                this._sql.Bind_Null(this, this._flags, index);
                return;
            }
            CultureInfo invariantCulture = CultureInfo.InvariantCulture;
            bool        flag1            = (this._flags & SQLiteConnectionFlags.BindInvariantText) == SQLiteConnectionFlags.BindInvariantText;
            CultureInfo cultureInfo      = CultureInfo.CurrentCulture;

            if ((this._flags & SQLiteConnectionFlags.ConvertInvariantText) == SQLiteConnectionFlags.ConvertInvariantText)
            {
                cultureInfo = invariantCulture;
            }
            if ((this._flags & SQLiteConnectionFlags.BindAllAsText) == SQLiteConnectionFlags.BindAllAsText)
            {
                if (value is DateTime)
                {
                    this._sql.Bind_DateTime(this, this._flags, index, (DateTime)value);
                    return;
                }
                this._sql.Bind_Text(this, this._flags, index, (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo)));
                return;
            }
            bool flag2 = (this._flags & SQLiteConnectionFlags.BindInvariantDecimal) == SQLiteConnectionFlags.BindInvariantDecimal;

            if ((this._flags & SQLiteConnectionFlags.BindDecimalAsText) == SQLiteConnectionFlags.BindDecimalAsText && value is decimal)
            {
                this._sql.Bind_Text(this, this._flags, index, (flag1 || flag2 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo)));
                return;
            }
            switch (dbType)
            {
            case DbType.Binary:
            {
                this._sql.Bind_Blob(this, this._flags, index, (byte[])value);
                return;
            }

            case DbType.Byte:
            {
                this._sql.Bind_UInt32(this, this._flags, index, Convert.ToByte(value, cultureInfo));
                return;
            }

            case DbType.Boolean:
            {
                this._sql.Bind_Boolean(this, this._flags, index, SQLiteConvert.ToBoolean(value, cultureInfo, true));
                return;
            }

            case DbType.Currency:
            case DbType.Double:
            case DbType.Single:
            {
                this._sql.Bind_Double(this, this._flags, index, Convert.ToDouble(value, cultureInfo));
                return;
            }

            case DbType.Date:
            case DbType.DateTime:
            case DbType.Time:
            {
                this._sql.Bind_DateTime(this, this._flags, index, (value is string?this._sql.ToDateTime((string)value) : Convert.ToDateTime(value, cultureInfo)));
                return;
            }

            case DbType.Decimal:
            {
                this._sql.Bind_Text(this, this._flags, index, (flag1 || flag2 ? SQLiteConvert.ToStringWithProvider(Convert.ToDecimal(value, cultureInfo), invariantCulture) : SQLiteConvert.ToStringWithProvider(Convert.ToDecimal(value, cultureInfo), cultureInfo)));
                return;
            }

            case DbType.Guid:
            {
                if (this._command.Connection._binaryGuid)
                {
                    SQLiteBase            sQLiteBase1           = this._sql;
                    SQLiteConnectionFlags sQLiteConnectionFlag1 = this._flags;
                    Guid guid = (Guid)value;
                    sQLiteBase1.Bind_Blob(this, sQLiteConnectionFlag1, index, guid.ToByteArray());
                    return;
                }
                this._sql.Bind_Text(this, this._flags, index, (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo)));
                return;
            }

            case DbType.Int16:
            {
                this._sql.Bind_Int32(this, this._flags, index, Convert.ToInt16(value, cultureInfo));
                return;
            }

            case DbType.Int32:
            {
                this._sql.Bind_Int32(this, this._flags, index, Convert.ToInt32(value, cultureInfo));
                return;
            }

            case DbType.Int64:
            {
                this._sql.Bind_Int64(this, this._flags, index, Convert.ToInt64(value, cultureInfo));
                return;
            }

            case DbType.Object:
            case DbType.String:
            {
                sQLiteBase           = this._sql;
                sQLiteConnectionFlag = this._flags;
                num = index;
                str = (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo));
                sQLiteBase.Bind_Text(this, sQLiteConnectionFlag, num, str);
                return;
            }

            case DbType.SByte:
            {
                this._sql.Bind_Int32(this, this._flags, index, Convert.ToSByte(value, cultureInfo));
                return;
            }

            case DbType.UInt16:
            {
                this._sql.Bind_UInt32(this, this._flags, index, Convert.ToUInt16(value, cultureInfo));
                return;
            }

            case DbType.UInt32:
            {
                this._sql.Bind_UInt32(this, this._flags, index, Convert.ToUInt32(value, cultureInfo));
                return;
            }

            case DbType.UInt64:
            {
                this._sql.Bind_UInt64(this, this._flags, index, Convert.ToUInt64(value, cultureInfo));
                return;
            }

            default:
            {
                sQLiteBase           = this._sql;
                sQLiteConnectionFlag = this._flags;
                num = index;
                str = (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo));
                sQLiteBase.Bind_Text(this, sQLiteConnectionFlag, num, str);
                return;
            }
            }
        }