仅在传递整数

时间:2016-06-10 15:17:53

标签: c# .net dapper sqlanywhere

目前,我正在尝试使用Named Parameters使用SAP Sybase SQL Anywhere 12与dapper合作。以下代码正确运行:

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();
        }
    }
}

但是当我将Str2 = "42"更改为某些string时,我得到以下异常:

  

将42a转换为不可能的整数

当我使用以下代码时抛出此异常:

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

有一些已知问题吗?这应该可以正常工作,因为我只想传递一个字符串。

修改

堆栈追踪:

  

iAnywhere.Data.SQLAnywhere.SAException(0x80004005):Umwandeln von'42a'auf integernichtmö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__61 1.MoveNext() bei System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) bei System.Linq.Enumerable.ToList[TSource](IEnumerable 1来源)      bei Dapper.SqlMapper.Query [T](IDbConnection cnn,String sql,Object param,IDbTransaction transaction,Boolean buffered,Nullable 1 commandTimeout, Nullable 1 commandType)      bei DapperSqlAnywhere.Program.Main(String [] args)在C:\ Users .... \ DapperSqlAnywhere \ Program.cs:Zeile 35。

非常感谢!

1 个答案:

答案 0 :(得分:0)

我对SQLAnywhere了解不多,但在第一个select语句中被解析为

SELECT 35 as Str1, 42 as Str2

这很好,因为它们都是整数 你的第二个陈述

SELECT 35 as Str1, 42a as Str2

应该是

SELECT 35 as Str1, '42a' as Str2

所以我尝试将代码更改为

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