我使用Dapper将参数附加到我的MySql查询时遇到了问题。 现在这可能是一个不好的问题,但我已经打了2个小时,现在仍然没有工作。使用Dapper附加参数到MySql
我的问题是在中间的SelectWithParametersTest()函数。这是我得到的...
编辑:好的更多细节。 实际的Mysql服务器抛出错误并说,“错误[07001] [MySQL] [ODBC 3.51驱动程序] [mysqld-5.1.61-0ubuntu0.11.10.1-log] SQLBindParameter不适用于所有参数”。
在QueryInternal <T
>(...)上它正在执行读取器的行上捕获到实际异常。 (使用(VAR读者= cmd.ExecuteReader())
当我检查命令没有连接到它的参数,但param对象(即传递给功能)具有我的匿名对象在它。
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; }
}
我不是在PC的权利,但:?实际的MySQL服务器:什么是真正与 – 2012-07-25 19:09:41
我的不好的事情发生抛出适合并且说,“错误[07001] [MySQL] [ODBC 3.51驱动程序] [mysqld-5.1.61-0ubuntu0.11.10.1-log] SQLBindParameter不用于所有参数”。 实际的异常在执行读取器的行上被QueryInternal(...)捕获。 (使用(var reader = cmd.ExecuteReader()) 当我检查命令时,没有附加参数,但参数对象在那里,也要感谢快速响应Marc。 –
Yojin
2012-07-25 19:19:57
我必须检查,但它看起来像MySql讨厌命名参数 - 另见http://stackoverflow.com/questions/1457597/mysql-rejecting-parameter。如果这是准确的,那么绑定这些可能是一个巨大的痛苦。 – 2012-07-25 19:26:24