2012-07-25 76 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