请查看以下代码
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语句的情况下添加参数存在一些问题。有什么工作可以传递参数吗?
答案 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)
{
}