如果查询涉及"如果存在"则参数.Add()不起作用。 c#中的语句

时间:2016-03-03 08:51:40

标签: c# sybase-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
{
    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 exists语句的情况下添加参数存在一些问题。有什么工作可以传递参数吗?

1 个答案:

答案 0 :(得分:0)

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

}