[英]Using dapper to attach parameters with MySql
I got a problem using dapper to attach parameters to my MySql queries. 我使用dapper将参数附加到MySql查询时遇到问题。 Now this may be a noobish problem, but I've beaten my head on this for the better part of 2 hours now and it's still not working. 现在这可能是一个愚蠢的问题,但是我现在已经在2小时的大部分时间里打败了我,但它仍然没有用。
My problem is with the SelectWithParametersTest() function right in the middle. 我的问题是在中间的SelectWithParametersTest()函数。 Here is what I've got... 这是我得到的......
EDIT: Ok more details. 编辑:更多细节。 The actual Mysql server throws fits and says, "ERROR [07001] [MySQL][ODBC 3.51 Driver][mysqld-5.1.61-0ubuntu0.11.10.1-log]SQLBindParameter not used for all parameters". 实际的Mysql服务器抛出拟合并说,“ERROR [07001] [MySQL] [ODBC 3.51驱动程序] [mysqld-5.1.61-0ubuntu0.11.10.1-log] SQLBindParameter不用于所有参数”。
The actual exception gets caught at QueryInternal <T
>(...) on the line where it's executing the reader. 在QueryInternal <T
>(...)执行读取器的行上捕获实际异常。 (using(var reader = cmd.ExecuteReader()) (使用(var reader = cmd.ExecuteReader())
When I inspect the command there are no parameters attached to it, but the param object (that was passed to the function) has my anon object in it. 当我检查命令时没有附加参数,但是param对象(传递给函数)中有我的anon对象。
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; }
}
Yokin,您是否尝试在C#代码中使用UInt32而不是长?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.