C# 如果查询涉及c中的“if exists”语句,则Parameters.Add()不起作用

C# 如果查询涉及c中的“if exists”语句,则Parameters.Add()不起作用,c#,sap-ase,C#,Sap Ase,请看一下下面的代码 string SearchQuery = @"if exists (select * from table where colName = @colNameVal) select 1 else select 0"; AseConnection connection = new AseConnection(); connection.ConnectionString = connectionString; connection.Open(); try { AseComm

请看一下下面的代码

string SearchQuery = @"if exists (select * from table where colName = @colNameVal) select 1 else select 0";
AseConnection connection = new AseConnection();
connection.ConnectionString = connectionString;
connection.Open();
try
{
    AseCommand selectCommand = new AseCommand(SearchQuery, connection);
    selectCommand.Parameters.Add(new AseParameter("@colNameVal", AseDbType.NVarChar, 13)).Value = "123";
    int doesValExist = (int)selectCommand.ExecuteScalar();
}
catch(Exception ex)
{

}
我正在使用Sybase.AdoNet4.AseClient。上面的代码正在引发异常,并显示以下消息

必须声明变量'@colNameVal'

但下面的查询正在从colName=@colNameVal的表中选择*


所以我假设在if-exists语句中添加参数会有一些问题。是否有任何方法可以传递参数?

尝试设置:selectCommand.CommandType=CommandType.text是否尝试了参数。AddWithValue尝试了两种建议,但都无效。得到同样的错误,他在那里。有关在中使用Sybase参数的信息,请参阅此链接。此链接中未提及netIf exists。您可以使用文字格式,也许这样可以。我已经好几年没有使用Sybase了,之前的代码是在SQL Server中使用的。这可以使用,但也有SQL注入的可能性,这就是我希望避免使用Sybase的原因。对上面的代码做了一点小小的更改。也许用那种方法。Sybase中的IF出口一定存在一些异常行为。我相信您已经考虑过的另一种选择是使用存储过程。
string SearchQuery = @"select count(*) from table where colName = @colNameVal";
AseConnection connection = new AseConnection();
connection.ConnectionString = connectionString;
connection.Open();
try
{
    AseCommand selectCommand = new AseCommand(SearchQuery, connection);
    selectCommand.Parameters.Add(new AseParameter("@colNameVal", AseDbType.NVarChar, 13)).Value = "123";
    int doesValExist = (int)selectCommand.ExecuteScalar();
}
catch(Exception ex)
{

}