Using named parameter only when passing integer

谁说我不能喝 提交于 2019-12-10 16:47:37

问题


Currently I'm trying to work with Named Parameters using SAP Sybase SQL Anywhere 12 with dapper. The following codes runs correctly:

public class Test
{
    public int Str1
    {
        get;
        set;
    }

    public string Str2
    {
        get;
        set;
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (SAConnection connection = new SAConnection("..."))
        {
            connection.Open();

            Test test = connection.Query<Test>("SELECT :Str1 as Str1, :Str2 as Str2",
                new Test() { Str1 = 35, Str2 = "42" }).FirstOrDefault();                    

            Console.WriteLine($"Str1: {test.Str1} | Str2: {test.Str2}");
            Console.ReadLine();
        }
    }
}

But when i change Str2 = "42" to some string, than i get the following exception:

Cast 42a to integer not possible

This exception is thrown when I'm using the following code:

Test test = connection.Query<Test>("SELECT :Str1 as Str1, :Str2 as Str2",
new Test() { Str1 = 35, Str2 = "42a" }).FirstOrDefault();  

Is there some known issue? This should work correctly, cause i just want to pass a string around.

Edit

Stack trace:

iAnywhere.Data.SQLAnywhere.SAException (0x80004005): Umwandeln von '42a' auf integer nicht möglich bei iAnywhere.Data.SQLAnywhere.SACommand._ExecuteReader(CommandBehavior commandBehavior, Boolean isExecuteScalar, Boolean isBeginExecuteReader) bei iAnywhere.Data.SQLAnywhere.SACommand.ExecuteDbDataReader(CommandBehavior behavior) bei System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) bei Dapper.SqlMapper.d__611.MoveNext() bei System.Collections.Generic.List1..ctor(IEnumerable1 collection) bei System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) bei Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType) bei DapperSqlAnywhere.Program.Main(String[] args) in C:\Users....\DapperSqlAnywhere\Program.cs:Zeile 35.

Thanks a lot!


回答1:


I don't know a great deal about SQLAnywhere but in your 1st select statement is being parsed as

SELECT 35 as Str1, 42 as Str2

which is fine because they're both integers Your 2nd statement

SELECT 35 as Str1, 42a as Str2

should probably be

SELECT 35 as Str1, '42a' as Str2

So I'd try changing the code to

Test test = connection.Query<Test>("SELECT :Str1 as Str1, ':Str2' as Str2",
            new Test() { Str1 = 35, Str2 = "42" }).FirstOrDefault();


来源:https://stackoverflow.com/questions/37751770/using-named-parameter-only-when-passing-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!