问题描述
小巧玲珑可以通过查询参数匿名对象,支持任何ADO.NET数据提供程序。然而,反对的Sybase 15 ADO.NET驱动程序运行下面的查询时:
Dapper can pass query parameters as anonymous objects, and supports any ADO.NET data provider. However, when running the following query against the Sybase 15 ADO.NET drivers:
using (var connection = new AseConnection("..."))
{
connection.Open();
var results = connection.Query<Foo>(
"dbo.sp_columns", new { table_name = "dbo.sysusers"},
commandType: CommandType.StoredProcedure);
}
...以下错误被抛出:
... the following error is thrown:
Sybase.Data.AseClient.AseException: Procedure sp_columns expects parameter @table_name, which was not supplied.
at Sybase.Data.AseClient.AseCommand.ᜁ(Int32 A_0)
at Sybase.Data.AseClient.AseCommand.ᜄ()
at Sybase.Data.AseClient.AseCommand.ᜀ(CommandBehavior A_0)
at Sybase.Data.AseClient.AseCommand.System.Data.IDbCommand.ExecuteReader()
at Dapper.SqlMapper.<QueryInternal>d__13`1.MoveNext() in SqlMapper.cs: line 579
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList(IEnumerable`1 source)
at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in SqlMapper.cs: line 536
更改到一个小巧玲珑的 DynamicParameters
设置@table_name也不起作用。
Changing it to a Dapper DynamicParameters
set with "@table_name" also doesn't work.
我怎样才能通强类型参数的Sybase与精致小巧的?
How can I pass strongly-typed parameters to Sybase with Dapper?
推荐答案
据我所知道的,原因似乎是是与Sybase的 AseParameter
类,不正确映射的DbType
到 AseDbType
值。
As far as I can tell, the reason seems to be something to do with Sybase's AseParameter
class, and not correctly mapping DbType
to AseDbType
values.
您可以解决的问题,通过定义自定义小巧玲珑 IDynamicParameters
设置,并与 AseParameter
填充它直接对象
You can work around the problem by defining a custom Dapper IDynamicParameters
set, and populating it with AseParameter
objects directly:
public class AseCommandParameters : List<AseParameter>, SqlMapper.IDynamicParameters
{
public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
foreach (var parameter in this)
command.Parameters.Add(parameter);
}
}
然后通过一个实例到查询:
And then passing an instance into the query:
using (var connection = new AseConnection("..."))
{
connection.Open();
var parameters = new AseCommandParameters
{
new AseParameter("@table_name", "dbo.sysusers")
};
var results = connection.Query<Foo>("dbo.sp_columns", parameters,
commandType: CommandType.StoredProcedure);
}
这工作,也可以指定 AseDbType
每个参数。
This works, and also lets you specify the AseDbType
for each parameter.
这篇关于如何使用精致小巧的强类型的查询参数与Sybase ASE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!