Reputation: 11
I am trying to use a C# SQL search and then get a Boolean result on whether or not the item was found. I have the search statement working but not the Boolean result portion.
EX: If i have a table with a column called @Names with the names, A,B,C but i search for Name D, how or can i get a Boolean result to come back and save false in a Boolean variable.
Upvotes: 0
Views: 391
Reputation: 727047
You can try this approach:
string query = @"
select case when exists (
select 1
from MyTable
where Name='D' -- This is the condition you are checking
) then 1 else 0 end";
bool exists;
using(var command = new SqlCommand(query, connection))
{
exists = Convert.ToBoolean(command.ExecuteScaler());
}
You can now use the ExecuteScalar
method, and cast the result to bool
for the result of your query.
Upvotes: 4