Пример #1
0
        public static int Update(SQRow table)
        {
            if (table.id == null)
            {
                return(0);
            }
            StringBuilder sb        = new StringBuilder();
            Type          tableType = table.GetType();
            var           fields    = tableType.GetFields();

            sb.Append("UPDATE ").Append(tableType.Name).Append(" SET ");

            foreach (var field in fields)
            {
                var value = field.GetValue(table);
                if (value != null)
                {
                    var converter = (TypeConverter)field.GetCustomAttributes(false)[0];
                    sb.Append(field.Name).Append(" = ").Append(converter.ToSqlString(value));
                    sb.Append(", ");
                }
            }

            sb.Remove(sb.Length - 2, 2);
            sb.Append(" WHERE id = ").Append(table.id).Append(";");

            if (Program.DEBUG)
            {
                Console.WriteLine(sb.ToString());
            }

            SQLiteCommand cmd = new SQLiteCommand(sb.ToString(), sqCon);

            return(cmd.ExecuteNonQuery());
        }
Пример #2
0
        public static void WriteRow(BinaryWriter writeTo, SQRow row)
        {
            try
            {
                Type tableType = row.GetType();

                string name = tableType.Name;
                writeTo.Write(name);

                var fields = tableType.GetFields();
                foreach (var field in fields)
                {
                    object obj = field.GetValue(row);
                    Type   t   = field.FieldType;

                    if (t == typeof(string))
                    {
                        writeTo.Write(obj == null ? "" : (string)obj);
                    }
                    else if (t == typeof(int))
                    {
                        writeTo.Write((int)obj);
                    }
                    else if (t == typeof(DateTime))
                    {
                        writeTo.Write(new DATETIME().ToSqlString(obj));
                    }
                }
            }
            catch { }
        }
Пример #3
0
        public static int Insert(SQRow table)
        {
            Type tableType = SQRow.GetTypeByName(table.GetType().Name);

            if (tableType == null)
            {
                throw new Exception("Invalid table type.");
            }

            StringBuilder sqString = new StringBuilder();
            StringBuilder values   = new StringBuilder();

            sqString.Append("INSERT INTO ").Append(tableType.Name).Append(" (");
            values.Append("VALUES (");

            var fields = tableType.GetFields();

            foreach (var field in fields)
            {
                var att   = field.GetCustomAttributes(false)[0];
                var value = field.GetValue(table);

                if (value != null)
                {
                    var    converter = (TypeConverter)att;
                    string sql       = converter.ToSqlString(value);

                    sqString.Append(field.Name).Append(", ");
                    values.Append(sql).Append(", ");
                }
            }
            //remove last commas
            sqString.Remove(sqString.Length - 2, 2);
            values.Remove(values.Length - 2, 2);
            //bring everything together
            sqString.Append(") ").Append(values.ToString()).Append(");");

            if (Program.DEBUG)
            {
                Console.WriteLine(sqString.ToString());
            }

            SQLiteCommand cmd = new SQLiteCommand(sqString.ToString(), sqCon);

            int count = 0;

            try
            {
                count = cmd.ExecuteNonQuery();
            }
            catch { }

            return(count);
        }