示例#1
0
        public int ExecuteNonQuery()
        {
            if (this._conn.Trace)
            {
                Console.WriteLine("Executing: " + this);
            }

            var r    = SQLite3.Result.OK;
            var stmt = this.Prepare();

            r = SQLite3.Step(stmt);
            this.Finalize(stmt);
            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(this._conn.Handle);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(this._conn.Handle);
                throw SQLiteException.New(r, msg);
            }
            else
            {
                throw SQLiteException.New(r, r.ToString());
            }
        }
示例#2
0
        public int ExecuteNonQuery(object[] source)
        {
            if (this.Connection.Trace)
            {
                Console.WriteLine("Executing: " + this.CommandText);
            }

            var r = SQLite3.Result.OK;

            if (!this.Initialized)
            {
                this.Statement   = this.Prepare();
                this.Initialized = true;
            }

            //bind the values.
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    SQLiteCommand.BindParameter(this.Statement, i + 1, source[i]);
                }
            }
            r = SQLite3.Step(this.Statement);

            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(this.Connection.Handle);
                SQLite3.Reset(this.Statement);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(this.Connection.Handle);
                SQLite3.Reset(this.Statement);
                throw SQLiteException.New(r, msg);
            }
            else
            {
                SQLite3.Reset(this.Statement);
                throw SQLiteException.New(r, r.ToString());
            }
        }
示例#3
0
        public T ExecuteScalar <T>()
        {
            if (this._conn.Trace)
            {
                Console.WriteLine("Executing Query: " + this);
            }

            T val = default(T);

            var stmt = this.Prepare();

            if (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var colType = SQLite3.ColumnType(stmt, 0);
                val = (T)this.ReadCol(stmt, 0, colType, typeof(T));
            }
            this.Finalize(stmt);

            return(val);
        }
示例#4
0
        public List <T> ExecuteQuery <T>(TableMapping map)
        {
            if (this._conn.Trace)
            {
                Console.WriteLine("Executing Query: " + this);
            }

            var r = new List <T>();

            var stmt = this.Prepare();

            var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

            for (int i = 0; i < cols.Length; i++)
            {
                var name = Marshal.PtrToStringUni(SQLite3.ColumnName16(stmt, i));
                cols[i] = map.FindColumn(name);
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = Activator.CreateInstance(map.MappedType);
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] == null)
                    {
                        continue;
                    }
                    var colType = SQLite3.ColumnType(stmt, i);
                    var val     = this.ReadCol(stmt, i, colType, cols[i].ColumnType);
                    cols[i].SetValue(obj, val);
                }
                r.Add((T)obj);
            }

            this.Finalize(stmt);
            return(r);
        }