dapper mysql 多参数查询_c# – 使用dapper通过MySql附加参数

本文档描述了在使用Dapper进行多参数查询时遇到的问题,特别是当尝试在MySQL查询中附加参数时。作者指出,查询内部在执行时抛出了一个SQLBindParameter异常,表明不是所有参数都被正确绑定。示例代码展示了如何创建和打开数据库连接,以及正常查询和带参数查询的用法。问题出在`SelectWithParametersTest()`函数中,该函数使用了一个匿名对象来传递参数,但遇到了异常。
摘要由CSDN通过智能技术生成

我使用dapper将参数附加到

MySql查询时遇到问题.

现在这可能是一个愚蠢的问题,但是我现在已经在2小时的大部分时间里对此表示不满,但它仍然无法正常工作.

我的问题是在中间的SelectWithParametersTest()函数.这是我得到的……

编辑:更多细节.

实际的Mysql服务器抛出拟合并说,“ERROR [07001] [MySQL] [ODBC 3.51驱动程序] [mysqld-5.1.61-0ubuntu0.11.10.1-log] SQLBindParameter不用于所有参数”.

在QueryInternal< T>(…)执行读取器的行上捕获实际异常. (使用(var reader = cmd.ExecuteReader())

当我检查命令时没有附加参数,但是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(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(parameterizedSQL, new { B = 50 });

this.PrintCitizens(result);

}

#endregion

#region Private Methods

private void PrintCitizens(IEnumerable 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值