2

我已经从这里的链接下载了SqlMapper.cs文件。

尽管我遇到了一些令人困惑的麻烦。每当我使用参数时,我的查询都会返回空集合,即使我很确定我使用的是相同的字符串数据。

这是用于提取数据的代码片段。

using (PhoenixConnection connection = new PhoenixConnection(Open: true))
{
    //this example works, returns 1 token object
    string queryWorks = @"Select AccountName, AccountToken from btsource.accounts
    where AccountName = 'RekindledPhoenix'";

    // replaces 'RekindledPhoenix' with a parameter, returns empty collection
    string queryDoesnt = @"Select AccountName, AccountToken from btsource.accounts 
    where AccountName = @AccountName";

    var tokenList = connection.Query<TokenRequest>(queryWorks);
    var tokenList = connection.Query<TokenRequest>(queryDoesnt, new { AccountName = "RekindledPhoenix" });
    return tokenList.FirstOrDefault();
}

这是我正在使用的课程...

public class TokenRequest
{
    public string AccountName { get; set; } //`AccountName` VARCHAR(50) NOT NULL
    public long AccountToken { get; set; } //`AccountToken` BIGINT(20) NOT NULL
}

这是我的 PhoenixConnection 对象中的包装函数。

public IEnumerable<T> Query<T>(string Query, dynamic Parameters = null)
{
    return _connection.Query<T>(Query, (object)Parameters);
}

我试过了:

  • 仔细检查mysql表名和值
  • 更改包装器以使用除动态之外的任何东西(不起作用)
  • 验证连接是否打开
  • 原始 mysqlConnection 对象、命令文本等

我会错过什么?

是否有一些用于参数的特殊设置?

编辑:

Using raw MySqlConnection parameters work better with ? and not @, though still doesn't work with Dapper.

I noticed there were specific lines within Dapper that extract parameters with regex ([@:]) statements, though question marks seem to be ignored. What should I change to give me the expected results?

4

1 回答 1

3

I found the issue. After using a raw MySqlConnection object to execute my queries, it lead me on a search for why @ symbols didn't work in any of my statements. In the past I've always used ?, so this behavior was odd to me.

Changing SqlMapper.cs

Change the following @ symbols to a ? (with approximate line numbers):

Line 1863: if (identity.sql.IndexOf("?" + prop.Name, StringComparison.InvariantCultureIgnoreCase) < 0

Line 1831: return parameters.Where(p => Regex.IsMatch(sql, "[?:]" + p.Name + "([^a-zA-Z0-9_]+|$)", RegexOptions.IgnoreCase | RegexOptions.Multiline));

Solved my problem!

于 2013-07-28T20:49:51.653 回答