我使用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