Hi,
How do you programmatically check the existence of a data in a SQL/MYSQL database? I have tried to do the followings, but it does not seem to work:
I have also tried casting but my compiler complains about it ...
So what does "SELECT COUNT(*)" return if data not found?
Thanks.
Henry
How do you programmatically check the existence of a data in a SQL/MYSQL database? I have tried to do the followings, but it does not seem to work:
Code:
string command = "SELECT COUNT(*) FROM table WHERE username = 'DoesNotExist';
MySqlCommand cmd = new MySqlCommand(command,connection);
if(cmd.ExecuteScalar()==null)[green]//does not work[/green]
{
[green]//Data not found[/green]
}else
{
[green]//Data found[/green]
}
I have also tried casting but my compiler complains about it ...
Code:
if(((int)cmd.ExecuteScalar())==0)
{
[green]//Data not found[/green]
}else
{
[green]//Data found[/green]
}
So what does "SELECT COUNT(*)" return if data not found?
Thanks.
Henry