I am trying to see if an ID exists in a database in this case it’s vetID.
To see if the result of the SQL query is successfull I am trying to use rdata.FieldCount
FieldCount always seems to return 11 even if the input ID (variable i) is = -400 (an id number that cannot exist in the database). I have also tried with other numbers that are not possible such as 100 or 800 (the database has only 1 – 5 items in it)
var mySQLCommand = new SqlCeCommand("SELECT * FROM vets WHERE vetID = @ID", dbCon);
mySQLCommand.Parameters.AddWithValue("ID", i);
SqlCeDataReader rdata = mySQLCommand.ExecuteReader();
if (rdata.FieldCount >= 1)
{
MessageBox.Show(" HAS ROWS "); go = true;
}
else
{
MessageBox.Show("NO ROWS RETURNED");
}
MessageBox.Show("rdata FieldCount " + rdata.FieldCount + " i has value " + i);
Is there a more elegant way to check an ID is still valid in the database and has not been deleted?
OK.. before you call me Stupid … im guessing the FieldCount is just the exactly that the FieldCount and not the FieldCount of the returned Query … doh…
But the question still stands is there an elegant way to know if the ID is still there in the database?
If you are using a datareader you can’t get a rowcount using fieldcount. But if you wanted to leave the query the same you’d do:
Another way to check is to use a
select count(*)to see if it’s in the table and that would be 0 if it doesn’t exist.