buff
buff

Reputation: 331

c# Nhibernate create query with dynamic parameters

using the folowing extention methods :

    public static void SetParameters(this IQuery query, List<object> Parameters) {
        for (int i = 0; i < Parameters.Count(); i++) {
            query.SetParameter(i, Parameters[i]);
        }
    }
    public static IQuery SetQuery(this ISession session, string Query, object[] Parameters) {
        return session.CreateSQLQuery(Query + string.Empty + "(" + ParseExt(Parameters) + ")");
    }

    private static string ParseExt(object[] Parameters) {
        var str = new List<string>();
        for (int i = 0; i < Parameters.Length; i++) {
            str.Add(":" + i);
        }
        return string.Join(",", str);
    }

i'm creating a query :

throws the following error :

Remember that ordinal parameters are 1-based!

NOTE : i tried to change the index to start from 1

Upvotes: 4

Views: 1077

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

The symbol for position-parameter is not :xxx but ?.

This change will work

private static string ParseExt(object[] Parameters) {
    var str = new List<string>();
    for (int i = 0; i < Parameters.Length; i++) {
        //str.Add(":" + i);
        str.Add("?"); // + i);
    }
    return string.Join(",", str);
}

Upvotes: 2

Related Questions