我在使用 dapper 将参数附加到 MySql 查询时遇到了问题。现在这可能是一个小问题,但我已经在这个问题上打了两个小时的大部分时间,但它仍然无法正常工作。
我的问题在于中间的 SelectWithParametersTest() 函数。这是我所拥有的...
编辑:好的更多细节。实际的 Mysql 服务器会说:“ERROR [07001] [MySQL][ODBC 3.51 Driver][mysqld-5.1.61-0ubuntu0.11.10.1-log]SQLBindParameter not used for all parameters”。
<T
实际的异常在执行阅读器的行上的QueryInternal >(...) 处被捕获。(使用(var reader = cmd.ExecuteReader())
当我检查命令时,它没有附加任何参数,但是 param 对象(传递给函数)中有我的 anon 对象。
using System;
using System.Data;
using System.Collections.Generic;
using Dapper;
class Program
{
static void Main(string[] args)
{
using (var dapperExample = new DapperExample())
{
//dapperExample.SelectTest();
dapperExample.SelectWithParametersTest();
}
}
}
class DapperExample : IDisposable
{
#region Fields
IDbConnection _databaseConnection;
#endregion
#region Constructor / Destructor
public DapperExample()
{
_databaseConnection = new System.Data.Odbc.OdbcConnection("DSN=MySqlServer;");
_databaseConnection.Open();
}
public void Dispose()
{
if (_databaseConnection != null)
_databaseConnection.Dispose();
}
#endregion
#region Public Methods (Tests)
public void SelectTest()
{
// This function correctly grabs and prints data.
string normalSQL = @"SELECT County as CountyNo, CompanyName, Address1, Address2
FROM testdb.business
WHERE CountyNo = 50 LIMIT 3";
var result = _databaseConnection.Query<ModelCitizen>(normalSQL);
this.PrintCitizens(result);
}
public void SelectWithParametersTest()
{
// This function throws OdbcException: "ERROR [07001] [MySQL][ODBC 3.51 Driver][mysqld-5.1.61-0ubuntu0.11.10.1-log]SQLBindParameter not used for all parameters"
string parameterizedSQL = @"SELECT County as CountyNo, CompanyName, Address1, Address2
FROM testdb.business
WHERE CountyNo = ?B";
var result = _databaseConnection.Query<ModelCitizen>(parameterizedSQL, new { B = 50 });
this.PrintCitizens(result);
}
#endregion
#region Private Methods
private void PrintCitizens(IEnumerable<ModelCitizen> citizenCollection)
{
foreach (var mc in citizenCollection)
{
Console.WriteLine("--------");
Console.WriteLine(mc.BankNo.ToString() + " - " + mc.CompNo.ToString());
Console.WriteLine(mc.CompanyName);
Console.WriteLine(mc.Address1);
Console.WriteLine(mc.Address2);
}
Console.ReadKey();
}
#endregion
}
public class ModelCitizen
{
public long CountyNo { get; set; }
public string CompanyName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
}