Ejemplo n.º 1
0
        public bool Save(SqlLiteData data)
        {
            if (string.IsNullOrEmpty(data.TableName))
            {
                return(false);
            }

            //check connection
            if (_connection.State == ConnectionState.Closed)
            {
                return(false);
            }

            //insert commands
            var _values = new Dictionary <string, string>();

            using (var _command = new SQLiteCommand(_connection)) {
                //insert params
                foreach (var param in data.DataTypes)
                {
                    _values.Add(param.Key, $"@{param.Key}");
                    _command.Parameters.Add($"@{param.Key}", param.Value.Key).Value = param.Value.Value;
                }
                _command.CommandText = $"" +
                                       $"insert into {data.TableName}({string.Join(",", _values.Keys.ToArray())}) " +
                                       $"values({string.Join(",", _values.Values.ToArray())})";
                _command.ExecuteNonQuery();
            }

            return(true);
        }
Ejemplo n.º 2
0
        public bool Update(SqlLiteData values, SqlLiteData condition = null)
        {
            if (string.IsNullOrEmpty(values.TableName))
            {
                return(false);
            }

            //check connection
            if (_connection.State == ConnectionState.Closed)
            {
                return(false);
            }

            //insert commands
            var _values    = new List <string>();
            var _condition = new List <string>();

            using (var _command = new SQLiteCommand(_connection))
            {
                //values
                foreach (var param in values.DataTypes)
                {
                    _values.Add($"{param.Key}=@{param.Key}");
                    _command.Parameters.Add($"@{param.Key}", param.Value.Key).Value = param.Value.Value;
                }

                //conditions
                if (condition?.DataTypes.Count > 0)
                {
                    foreach (var param in condition?.DataTypes)
                    {
                        _condition.Add($"{param.Key}=@{param.Key}");
                        _command.Parameters.Add($"@{param.Key}", param.Value.Key).Value = param.Value.Value;
                    }
                }

                //update table
                _command.CommandText = $"" +
                                       $"update {values.TableName} set {string.Join(",", _values.ToArray())} " +
                                       (_condition.Count > 0 ?
                                        $"where {string.Join(" and ", _condition.ToArray())}" : "");
                _command.ExecuteNonQuery();
            }

            return(true);
        }