Пример #1
0
        private void BindParameters3(IntPtr pStmt)
        {
            if (sql_params == null)
            {
                return;
            }
            if (sql_params.Count == 0)
            {
                return;
            }

            int pcount    = Sqlite3.BindParameterCount(pStmt);
            var _replaces = "@?:$".ToCharArray();

            for (int i = 1; i <= pcount; i++)
            {
                var name = Sqlite3.BindParameterName(pStmt, i);

                foreach (var _replace in _replaces)
                {
                    name = name.Replace(_replace.ToString(), "");
                }

                SqliteParameter param = null;
                if (name != null)
                {
                    param = sql_params[name] as SqliteParameter;
                }
                else
                {
                    param = sql_params[i - 1] as SqliteParameter;
                }

                if (param.Value == null)
                {
                    Sqlite3.BindNull(pStmt, i);
                    continue;
                }

                Type ptype = param.Value.GetType();
#if WINDOWS_PHONE_APP
                if (ptype.GetTypeInfo().IsEnum)
#else
                if (ptype.IsEnum)
#endif
                { ptype = Enum.GetUnderlyingType(ptype); }

                SqliteError err;

                if (ptype.Equals(typeof(String)))
                {
                    String s = (String)param.Value;
                    err = (SqliteError)Sqlite3.BindText(pStmt, i, s, -1, IntPtr.Zero);
                }
                else if (ptype.Equals(typeof(DBNull)))
                {
                    err = (SqliteError)Sqlite3.BindNull(pStmt, i);
                }
                else if (ptype.Equals(typeof(Boolean)))
                {
                    bool b = (bool)param.Value;
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, b ? 1 : 0);
                }
                else if (ptype.Equals(typeof(Byte)))
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (Byte)param.Value);
                }
                else if (ptype.Equals(typeof(Char)))
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (Char)param.Value);
                }
#if WINDOWS_PHONE_APP
                else if (ptype.GetTypeInfo().IsEnum)
#else
                else if (ptype.IsEnum)
#endif
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (Int32)param.Value);
                }
                else if (ptype.Equals(typeof(Int16)))
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (Int16)param.Value);
                }
                else if (ptype.Equals(typeof(Int32)))
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (Int32)param.Value);
                }
                else if (ptype.Equals(typeof(SByte)))
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (SByte)param.Value);
                }
                else if (ptype.Equals(typeof(UInt16)))
                {
                    err = (SqliteError)Sqlite3.BindInt(pStmt, i, (UInt16)param.Value);
                }
                else if (ptype.Equals(typeof(DateTime)))
                {
                    DateTime dt = (DateTime)param.Value;
                    err = (SqliteError)Sqlite3.BindText(pStmt, i, dt.ToString("yyyy-MM-dd  HH:mm:ss"), -1, IntPtr.Zero);
                }
                else if (ptype.Equals(typeof(Decimal)))
                {
                    err = (SqliteError)Sqlite3.BindDouble(pStmt, i, Decimal.ToDouble((Decimal)param.Value));
                }
                else if (ptype.Equals(typeof(Double)))
                {
                    err = (SqliteError)Sqlite3.BindDouble(pStmt, i, (Double)param.Value);
                }
                else if (ptype.Equals(typeof(Single)))
                {
                    err = (SqliteError)Sqlite3.BindDouble(pStmt, i, (Single)param.Value);
                }
                else if (ptype.Equals(typeof(UInt32)))
                {
                    err = (SqliteError)Sqlite3.BindInt64(pStmt, i, (UInt32)param.Value);
                }
                else if (ptype.Equals(typeof(Int64)))
                {
                    err = (SqliteError)Sqlite3.BindInt64(pStmt, i, (Int64)param.Value);
                }
                else if (ptype.Equals(typeof(Byte[])))
                {
                    err = (SqliteError)Sqlite3.BindBlob(pStmt, i, (byte[])param.Value, ((byte[])param.Value).Length, IntPtr.Zero);
                }
                else if (ptype.Equals(typeof(Guid)))
                {
                    err = (SqliteError)Sqlite3.BindText(pStmt, i, param.Value.ToString(), param.Value.ToString().Length, IntPtr.Zero);
                }
                else
                {
                    throw new ApplicationException("Unkown Parameter Type");
                }
                if (err != SqliteError.OK)
                {
                    throw new ApplicationException("Sqlite error in bind " + err);
                }
            }
        }