【发布时间】:2012-07-25 19:05:58
【问题描述】:
我在使用 dapper 将参数附加到 MySql 查询时遇到问题。 现在这可能是一个小问题,但我已经为此苦苦挣扎了 2 个小时,但它仍然无法正常工作。
我的问题在于中间的 SelectWithParametersTest() 函数。这是我得到的...
编辑:好的更多细节。 实际的 Mysql 服务器会说:“ERROR [07001] [MySQL][ODBC 3.51 Driver][mysqld-5.1.61-0ubuntu0.11.10.1-log]SQLBindParameter not used for all parameters”。
实际的异常在 QueryInternal<T>(...) 在它执行读取器的行上被捕获。 (使用(var reader = cmd.ExecuteReader())
当我检查命令时,它没有附加任何参数,但参数对象(传递给函数)中有我的匿名对象。
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; }
}
【问题讨论】:
-
我现在不在电脑前,但是:这到底会发生什么?
-
我的错:实际的 Mysql 服务器不正常并说:“错误 [07001] [MySQL][ODBC 3.51 驱动程序][mysqld-5.1.61-0ubuntu0.11.10.1-log]SQLBindParameter not用于所有参数”。实际的异常被 QueryInternal
(...) 捕获到它正在执行读取器的行上。 (使用(var reader = cmd.ExecuteReader())当我检查命令时没有附加参数,但参数对象在那里。也感谢 Marc 的快速响应。 -
我将不得不检查,但它看起来像 MySql 讨厌命名参数 - 另请参阅 stackoverflow.com/questions/1457597/mysql-rejecting-parameter 。如果这是准确的,那么绑定这些可能会很痛苦。
-
啊,是的。整个位置参数与命名参数真的很难解析。就我个人而言,我看到了代码,我仍然认为你正在做的反射是黑魔法。我将使用 MySqlConnector 驱动程序而不是 ODBC 再试一次,如果能解决问题,我会告诉你。
-
好的,使用 MySqlConnectorNet 6.4.3 解决了我的一个问题,但现在导致了其他问题。 (解析第 0 列时出错(CountyNo=50 - UInt32))。我会尝试解决这个问题,如果不行,我会发布一个新问题。