我已经从链接 here 下载了 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();
}
这是我正在使用的类(class)...
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 对象、命令文本等
我可能缺少什么?
是否有一些特殊的参数设置?
编辑:
使用原始 MySqlConnection
参数与 ?
配合使用效果更好,而不是与 @
配合使用,但仍无法与 Dapper 配合使用。
我注意到 Dapper 中有一些特定的行使用正则表达式 ([@:]
) 语句提取参数,但问号似乎被忽略了。我应该改变什么才能得到预期的结果?
最佳答案
我发现了这个问题。使用原始MySqlConnection
后对象执行我的查询,它引导我寻找原因 @
符号在我的任何陈述中都不起作用。过去我一直用?
,所以这种行为对我来说很奇怪。
更改 SqlMapper.cs
更改以下内容 @
符号到 ?
(带有大概的行号):
第 1863 行:if (identity.sql.IndexOf("
? " + prop.Name, StringComparison.InvariantCultureIgnoreCase) < 0
第 1831 行:return parameters.Where(p => Regex.IsMatch(sql, "[
? :]" + p.Name + "([^a-zA-Z0-9_]+|$)", RegexOptions.IgnoreCase | RegexOptions.Multiline));
解决了我的问题!
关于c# - 参数不适用于 Dapper.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17905454/