Saturday, October 30, 2010

Incorrect Syntax near % sql parameter (C#)

When writing sql statements in your business logic layer, you often want to append % which is a literal in your query. An example would be like this:
Code:
string query = "Select * from Patient where LastName like '" + "@family" + " %'";
However, this would give you an error, since the @family parameter is inside the tick marks that makes it a literal, but the % sign should be inside the tick marks. So, the correct sql syntax would be this way:
Code:
string query = "Select * from Patient where LastName like (@family + '%')";

Source: VBForums Link

2 comments: