0

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.

4

1 に答える 1

4

このアプローチを試すことができます:

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());
}

メソッドを使用して、結果をクエリの結果ExecuteScalarにキャストできるようになりました。bool

于 2012-06-01T01:28:11.000 に答える