2012-07-25 78 views
5

我使用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; } 
} 
+0

我不是在PC的權利,但:?實際的MySQL服務器:什麼是真正與 – 2012-07-25 19:09:41

+0

我的不好的事情發生拋出適合並且說,「錯誤[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

+0

我必須檢查,但它看起來像MySql討厭命名參數 - 另見http://stackoverflow.com/questions/1457597/mysql-rejecting-parameter。如果這是準確的,那麼綁定這些可能是一個巨大的痛苦。 – 2012-07-25 19:26:24

回答

0

Yokin,你有沒有嘗試在C#代碼中使用UInt32的,而不是長期

+0

這應該是一個評論,而不是一個答案 – 2012-08-07 21:59:44

+0

我怎麼評論,我沒有看到一個按鈕(除了這個評論) – udog 2012-08-07 22:08:37

+0

該文本框不顯示,因爲有太多的評論,但下面的評論,有一個按鈕說「添加/顯示3更多評論」。點擊它,它會顯示文本框 – 2012-08-07 23:17:55