我在 C# 项目中使用 Castle ActiveRecord,但遇到了一个奇怪的错误。我想通过 ActiveRecord 方法从指定列中检索数据,以便将其放入组合框中。
我使用这种方法进行简单的查询:
...
using Castle.ActiveRecord.Queries;
...
public static Lable[] ReturnAllLableNames()
{
SimpleQuery<Lable> q = new SimpleQuery<Lable>(typeof(Lable), @"
Select LableName
from Lable
");
return q.Execute();
}
其中 Lable 是一个表,也是我的项目中的一个类,它具有 LableID 和 LableName 列。
我第一次构建并尝试运行我的项目时,VS 要求我指定 ActiveRecordBaseQuery.cs 文件,当然我没有,所以我按了“取消”并重新构建了我的项目。下次我遇到以下错误时:
Could not perform ExecuteQuery for Lable
错误详细信息
我的类
我不知道应该在哪里搜索我的错误,因为我使用了 SimpleQuery 示例中的这段代码,并且添加了对我的类的所有必要引用。
第二个想法是,是否有其他方法可以使用 Castle AR 方法检索指定的列?
我很高兴听到有关我的问题的任何线索。
I'm using Castle ActiveRecord for my project on C# and I have a weird error. I want to retrieve data from one specified column via ActiveRecord methods, so that I could put it into comboBox.
I use this method with simple query:
...
using Castle.ActiveRecord.Queries;
...
public static Lable[] ReturnAllLableNames()
{
SimpleQuery<Lable> q = new SimpleQuery<Lable>(typeof(Lable), @"
Select LableName
from Lable
");
return q.Execute();
}
where Lable is a table and also a class in my project, which has LableID and LableName columns.
The first time I built and tried to run my project, VS asked me to specify the ActiveRecordBaseQuery.cs file, which of course I didn't have, so I pressed "Cancel" and rebuilt my project. The next time I had a following error:
Could not perform ExecuteQuery for Lable
error details
my class
I have no clues where I should search for my error, because I used this code from SimpleQuery examples and I added all necessary references to my class.
On the second thought, is there any other way to retrieve one specified column using Castle AR methods?
I'd be happy to hear any clues regarding my problem.
发布评论
评论(1)
NHibernate 的 HQL 与 SQL 不同,属性和类名区分大小写。在您的情况下,该属性称为
labelName
,而不是查询中所述的LabelName
。我建议将属性名称更改为LabelName
(请参阅 .net设计指南)。另外,如果您使用的是 C# 3.0+,您可能需要使用自动实现的属性。
另外,IIRC 您需要在查询中使用完全限定的属性,例如
select l.LabelName from Label l
最后,如果您打算返回字符串列表 (
LabelName
s),您需要更改方法的签名,并使用SimpleQuery
而不是SimpleQuery
。请参阅 SimpleQuery 文档以供参考。NHibernate's HQL, unlike SQL, is case sensitive for properties and class names. In your case, the property is called
labelName
, notLabelName
as it says in the query. I recommend changing the property name toLabelName
(see .net design guidelines).Also, if you're using C# 3.0+ you might want to use auto-implemented properties.
Also, IIRC you need to use fully-qualified properties in the query, e.g.
select l.LabelName from Label l
Finally, if you mean to return a list of strings (
LabelName
s), you need to change the signature of the method and also useSimpleQuery<string>
instead ofSimpleQuery<Label>
. See the SimpleQuery docs for reference.