Example #1
0
        public int Update(IEntityModel entity)
        {
            var columns = entity.Columns.Where(x => x != entity.PrivateKey);

            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("UPDATE {0}", entity.TableName);
            sql.AppendFormat(" SET {0}", string.Join(" , ", columns.Select(x => x + " = ?")));
            sql.AppendFormat(" WHERE {0} = ?", entity.PrivateKey);

            SQLiteCommand cmd = _conn.Command as SQLiteCommand;

            cmd.CommandText = sql.ToString();
            cmd.Parameters.Clear();

            object[] values = entity.GetRow();
            for (int i = 0; i < values.Length; i++)
            {
                var param = cmd.CreateParameter();
                param.Value = values[i];

                cmd.Parameters.Add(param);
            }

            var keyparam = cmd.Parameters[0];

            cmd.Parameters.Add(keyparam);
            cmd.Parameters.RemoveAt(0);

            return(cmd.ExecuteNonQuery());
        }
Example #2
0
        public int Insert(IEntityModel entity)
        {
            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("INSERT INTO {0}", entity.TableName);
            sql.AppendFormat(" VALUES ( {0} )", string.Join(" , ", entity.Columns.Select(x => "?")));

            SQLiteCommand cmd = _conn.Command as SQLiteCommand;

            cmd.CommandText = sql.ToString();
            cmd.Parameters.Clear();

            object[] values = entity.GetRow();
            for (int i = 0; i < values.Length; i++)
            {
                var param = cmd.CreateParameter();
                param.Value = values[i];

                cmd.Parameters.Add(param);
            }

            return(cmd.ExecuteNonQuery());
        }