示例#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 SQRow[] Select(string table, int startIndex, int lenght)
        {
            Type   tableType = SQRow.GetTypeByName(table);
            string s         = $"SELECT * FROM {tableType.Name} LIMIT {startIndex}, {lenght};";

            SQLiteCommand    cmd  = new SQLiteCommand(s, sqCon);
            SQLiteDataReader data = cmd.ExecuteReader();

            return(ReadSQData(data, tableType));
        }
示例#4
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);
        }
示例#5
0
        public static SQRow Select(string table, int id)
        {
            Type   tableType = SQRow.GetTypeByName(table);
            string s         = $"SELECT * FROM {tableType.Name} WHERE id = {id};";

            SQLiteCommand    cmd  = new SQLiteCommand(s, sqCon);
            SQLiteDataReader data = cmd.ExecuteReader();

            var result = ReadSQData(data, tableType);

            if (result.Length > 0)
            {
                return(result[0]);
            }

            return(null);
        }