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