Skip to content

Instantly share code, notes, and snippets.

@marek-stoj
Created January 10, 2012 16:07
Show Gist options
  • Save marek-stoj/1589779 to your computer and use it in GitHub Desktop.
Save marek-stoj/1589779 to your computer and use it in GitHub Desktop.
Problem with dynamic parameters in Dapper
When running this code, I get SqlException with this message: "Incorrect syntax near '@MaxCount'.". Why isn't the @MaxCount parameter replaced with the given value?
public IEnumerable<ArticleInfo> GetPopularArticles(Date fromDate, Date toDate, int maxCount)
{
var popularArticles = new List<ArticleInfo>();
IEnumerable<dynamic> rawArticles;
using (var connection = CreateOpenedConnection())
{
string queryText =
" select top @MaxCount" +
" au.Url," +
" am.Title," +
" av.[Count] as ViewsCount" +
" av.[Date]" +
" from ArticleViews av" +
" left join ArticleUrls au on av.ArticleId = au.Id" +
" left join ArticleMetadata am on am.ArticleId = au.Id" +
" order by av.[Count] desc, av.[Date] desc, am.Title, au.Id";
rawArticles =
connection.Query(
queryText,
new { MaxCount = maxCount });
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment