Wrong for AddNamedParameter with statement contain percent character

40-beta
.net

#1

I have snip code:

var keyword = "ninh";
var page = 1;
var pageSize = 10;
var bucketName = "myBucket";
string statement = "SELECT `" + bucketName + "`.* FROM `" + bucketName
                                     + "` WHERE type = 'myobject'"
                                     + " AND name LIKE '%$keyword%' AND status <> -1"
                                     + " LIMIT $limit"
                                     + " OFFSET $offset";
var query = new QueryRequest(statement)
                .AddNamedParameter("$keyword", keyword)
                .AddNamedParameter("$offset", page)
                .AddNamedParameter("$limit", pageSize);

But query string after AddNamedParameter:

SELECT `myBucket`.* FROM `myBucket` WHERE type = 'myobject' AND name LIKE '%25$keyword%25' AND status <> -1 LIMIT $limit OFFSET $offset&$keyword="ninh"&$offset=1&$limit=10

How can my statement can contain ‘%’ character for condition LIKE?


#2

Hi @Sonrobby,
Here is a sample Method for parameter query in C#.

 private void ExecuteSampleQuery(string value, int limit)
    {
        var queryRequest = new QueryRequest()
            .Statement("SELECT name FROM " + this.Bucket.Name + " WHERE type LIKE $type LIMIT $limit")
            .AddNamedParameter("limit", limit)
            .AddNamedParameter("type", value);

        Console.WriteLine("Sample N1QL Query:");

        ExecuteQuery(queryRequest)
            .Rows
            .ForEach(row =>
            {
                Console.WriteLine(row);
            });

        Console.WriteLine("Query completed");
    }

I hope this helps