コード例 #1
0
        public int Insert(ECTable _table)
        {
            Type t = _table.GetType();

            PropertyInfo[] properties = t.GetProperties().Where(x => x.IsDefined(typeof(ECTableFieldAttribute), false)).ToArray();

            string sql = "INSERT INTO `" + t.Name + "` (";

            foreach (PropertyInfo p in properties.Where(x => !x.IsDefined(typeof(ECAutoIncrementAttribute), false)))
            {
                sql += "`" + p.Name + "`,";
            }
            sql = sql.Substring(0, sql.Length - 1); //delete last comma

            sql += ") VALUES (";
            foreach (PropertyInfo p in properties.Where(x => !x.IsDefined(typeof(ECAutoIncrementAttribute), false)))
            {
                sql += _table.GetValueInSqlFormat(p) + ",";
            }
            sql = sql.Substring(0, sql.Length - 1); //delete last comma

            sql += ");";

            using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                cmd.ExecuteNonQuery();

            sql = "SELECT MAX(RecId) AS RecId FROM " + _table.GetType().Name;
            using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                using (MySqlDataReader res = cmd.ExecuteReader())
                {
                    res.Read();
                    return(res.GetInt32(nameof(_table.RecId)));
                }
        }
コード例 #2
0
        public void Modify(ECTable _table)
        {
            Type t = _table.GetType();

            PropertyInfo[] properties = t.GetProperties().Where(x => x.IsDefined(typeof(ECTableFieldAttribute), false)).ToArray();
            string         sql        = "UPDATE `" + t.Name + "` SET ";

            foreach (PropertyInfo p in properties)
            {
                sql += "`" + p.Name + "`=" + _table.GetValueInSqlFormat(p) + ",";
            }
            sql = sql.Substring(0, sql.Length - 1) + " WHERE RecId=" + _table.RecId + ";";

            command.CommandText = sql;
            command.ExecuteNonQuery();
        }