简体   繁体   English

如何编写一种以列表结构从Matisse表返回所有记录的方法?

[英]How can I write a method that returns all records from a Matisse table in a list structure?

Very new to using Matisse so I don't know if I'm just flat out doing this wrong or something. 使用Matisse非常陌生,所以我不知道我是否只是在做错了事或其他事情。 I have a table containing usernames and passwords for the system, along with the following method... 我有一个表,其中包含系统的用户名和密码以及以下方法...

    public List<Users> FindUsers()
    {

        List<Users> users = new List<Users>();
        //Users obj;
        executeCmd("SELECT * FROM Users");
        while (Reader.Read())
        {
            Users obj = new Users(db, 0x10ff);
            users.Add(obj);
        }
        reader.Close();
        return users;

    }

Which I had hoped would simply pull all records from the table, drop them in a list and return that list. 我希望可以简单地从表中提取所有记录,将它们放入列表中并返回该列表。 The problem however lies with this line of code here. 但是问题出在这里的这行代码。

    Users obj = new Users(db, 0x10ff);

I can only seem to pull one record, entering '0x10ff' as a parameter(the records OID) will return that record, which works with any record I try. 我似乎只能拉一条记录,输入'0x10ff'作为参数(记录OID)将返回该记录,该记录适用于我尝试的任何记录。 Leaving it blank(ie '(db)' ) throws a Matisse exception(InvalidOperation) and entering anything else other than a specific records OID returns an ObjectNotFound exception. 将其保留为空白(即'(db)')将引发Matisse异常(InvalidOperation),并输入除特定记录以外的其他任何内容OID返回ObjectNotFound异常。 Am I just doing something totally stupid here? 我在这里只是做些完全愚蠢的事情吗? What am I missing? 我想念什么?

Thanks 谢谢

EDIT: db refers to the database connection, shown below, DBAcess being a class with connectivity & query functionality. 编辑:db表示数据库连接,如下所示,DBAcess是具有连接性和查询功能的类。

    private MtDatabase db;

    public DBAccess()
    {
        db = new MtDatabase(Properties.Settings.Default.Host, Properties.Settings.Default.Database);
        db.Open();
    }

And as for the constructers, I left them as the default ones when I imported the classes from Matisse 至于构造器,当我从Matisse导入类时,将它们保留为默认构造器

//  Generated constructor, do not modify
/// <summary>
/// The factory constructor
/// </summary>
/// <param name="db">a database</param>
/// <param name="mtOid">an existing object ID in the db</param>
public Users(MtDatabase db, int mtOid) : 
        base(db, mtOid) {
}

//  Generated constructor, do not modify
/// <summary>
/// Cascaded constructor, used by subclasses to create a new object in the database
/// </summary>
/// <param name="clsObj">the class descriptor of the class to instantiate</param>
protected Users(MtClass clsObj) : 
        base(clsObj) {
}


#endregion

//  GEN_END: Matisse Generated Code - Do not modify


//  Generated constructor
/// <summary>
/// Default constructor provided as an example. NOTE: You may modify or delete this constructor
/// </summary>
/// <param name="db">a database</param>
public Users(MtDatabase db) : 
        base(GetClass(db)) {
}

Have now sorted this out, turns out I was just doing it wrong. 现在已经解决了这个问题,原来我只是做错了。

Instead of trying to retrieve the values with the reader, if you instead return the object and then use that to create a new User containing the values of said object, that works. 如果您改为返回对象,然后使用该对象来创建一个包含所述对象的值的新用户,则可以尝试使用读取器来检索这些值,而可以。 Not sure if that really explains it all that well so here is the updated, working code. 不确定是否真的能很好地解释所有问题,所以这里是更新的有效代码。

    public List<Users> FindUsers()
    {
        List<Users> users = new List<Users>();
        executeCmd("SELECT REF(Users) FROM Users c");
        while (Reader.Read())
        {
            MtObject obj = Reader.GetObject(0);
            users.Add(new Users(db, obj.MtOid));
        }
        Reader.Close();
        return users;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以编写一个查询来返回数据库中没有的固定列表中的所有值吗? - Can I write a query that returns all the values from a fixed list that are not in the database? 我想知道如何编写一个方法来返回从 1 到作为参数传递的数字的所有整数的总和 - I want to know how to write a method that returns the sum of all integers from 1 up to the number passed as parameter 如何编写一个返回6个字符长度的唯一字符串的方法? - How can I write a method that returns an unique string of 6 characters length? 如何编写返回 xaml 切换开关的值的方法? - How can I write a method that returns the value of a of xaml toggle switch? 如何将记录列表传递给方法并跳过所有传递的记录 - How to Pass the list of records to a method and skip all the passed records 如何从列表数组(linq 列表)C# 中的所有元素调用方法 - How can I call method from all elements in list array (linq list) C# 如何编写C#方法,该方法将使用String []和List检索目录树中的所有目录 <String[]> ? - How can I write a C# method which will retrieve all directories in a directory tree using String[] and List<String[]>? 如何显示记录列表中的单个记录 - How can I display the single record from a list of records 在对表,其布局和记录建模时,如何避免代码重复,所有这些都共享相同的基本结构? - How do I avoid code duplication when modelling a table, its layout, and its records, all of which share the same basic structure? 如何使用C#将数据库中的所有表的所有记录导出到json中 - How can I export all table all records in a database into json using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM