Parameters.Add() 在涉及 c# 中的“if exist”语句的查询中不起作用

本文关键字:exist if 语句 不起作用 查询 中的 Add Parameters | 更新日期: 2023-09-27 18:37:06

请看下面的代码

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。上面的代码抛出 AseException 并显示以下消息

" 必须声明变量 '@colNameVal' "。

但是以下查询select * from table where colName = @colNameVal工作。

所以我假设在 if 存在语句的情况下添加参数存在一些问题。有什么解决方法可以传递参数吗?

Parameters.Add() 在涉及 c# 中的“if exist”语句的查询中不起作用

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)
{
}