I'm somewhat new to NHibernate, but I've looked all around and can't figure out what's going wrong. I'm running NHibernate 1.2, and connecting to a MSSQL 2005 db.
I need to inject a custom SQL criterion into an otherwise normal ICriteria.List() call, and provide a parameter value.
It seems like NHibernate.Expression.SQLCriterion is designed for exactly this, but it always gives me a "Incorrect syntax near '?'." exception, as though it's just not parsing out the "?" param, or not creating the command param to go with it. The positional param is being included, because I can see it in the exception log, but SQL doesn't know what to do with that "?"
What's going wrong here, or what alternative method should I be using?
This is all split out into some abstract classes and methods, so in this context I just have a single method to return a list of ICriterions that will be added to another ICriteria.
Here is the code for that method:
The "_sqlText" value is a piece of a WHERE clause from a config setting that does a simple LIKE comparison.
Code:
public override List<NHibernate.Expression.ICriterion> GetCriteria()
{
string matchParamText = "%" + _matchText + "%";
int count = _sqlText.Split('?').Length - 1;
object[] paramValues = new object[count];
NHibernate.Type.IType[] paramTypes = new NHibernate.Type.IType[count];
for (int i = 0; i < count; i++)
{
paramValues[i] = matchParamText;
paramTypes[i] = NHibernate.NHibernateUtil.String;
}
List<NHibernate.Expression.ICriterion> c = new List<NHibernate.Expression.ICriterion>();
c.Add(
new NHibernate.Expression.SQLCriterion(
new NHibernate.SqlCommand.SqlString(_sqlText),
paramValues,
paramTypes
)
);
return c;
}
Thanks.