To answer your subject line first, since it isn;t related to teh question body:
Use
TryParse[
^] or
TryParseExact[
^]:
The code in your body will not work: typing an SQL command string and expecting that to automatically know which connection it should use, connect to the server, execute a command, pick a single result from the long list of returned values, and then change that to an integer is just wishful thinking - it just isn't going to happen.
Instead, you need to look at your DB and work out exactly what is in it, and what you want to return - because I can't honestly see anything in that code which looks like it was planned instead of thrown together. None of it will work; all of it will need thinking about.
Sorry if that sounds brutal - it does, but it's meant kindly - but guessing and hoping are not a viable design method: you need to think about your data and what you want to get out of it very carefully before you even start to code. I can't even work out from that what you are trying to do, much less help you fix it!