[英]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.