0

I am trying to write a program in Java which has to check if SQL Server (2008) is installed on a particular machine. Is checking the registry entry the only solution? I prefer a file-based solution.

Currently, I am checking for the presence of OSQL.exe (which I use to execute queries and SQL scripts) in the <Installation Path>\100\Tools\Binn folder. What does the 100 specify? Can it be hardcoded in the program, or will I have to search for the OSQL.exe file starting from the installation path?

GPX
  • 3,506
  • 10
  • 52
  • 69
  • 1
    `100` is the version of SQL server, and specifically means you're dealing with a `2008` or `2008 R2` installation (Internally, 2008 is version 10.0, but they remove the "." in the path). `2005` installed primarily under a `90` directory, `2000` under `80`, etc. – Damien_The_Unbeliever Aug 12 '11 at 05:59
  • If you're already having to consult the registry (you *are* doing that, to find where it's been installed, aren't you?), then why is a subsequent file search going to "improve" the result. – Damien_The_Unbeliever Aug 12 '11 at 06:01
  • @Damien: Thanks for the heads up. And about the registry check - No, I am not doing that! – GPX Aug 12 '11 at 06:20

2 Answers2

0

To me this method is messy and full of several unseen pitfalls. I would prefer the user to assert if the db is present and if yes, ask for the connection URL, username and pass using which you can try to create a simple jdbc connection to check if its running.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
FUD
  • 5,114
  • 7
  • 39
  • 61
  • Well before asking the user for the server and DB details, I'd first like to check if SQL Server is installed. I'd like the user to provide the same path he/she used during SQL Server installation (for example, `C:\Program Files\Microsoft SQL Server`) and from then on, figure out, through code, where the `OSQL.exe` file is. – GPX Aug 12 '11 at 06:04
0

Your question is a bit unclear, in particular it's not obvious if you need to enumerate local and/or remote instances? For local instances searching the registry is more reliable than scanning the file system, for remote instances sqlcmd.exe can list instances on the network. This page gives a good overview of the options (for 2005, but they are still valid for 2008).

If it's possible for you to call .NET from Java, you can use the Smo Server object to get a lot of information about the instances, including installation paths. There are numerous questions on this site about enumerating instances and gathering information about them.

Finally, please note that osql.exe is deprecated so you should be using sqlcmd.exe instead.

Community
  • 1
  • 1
Pondlife
  • 15,992
  • 6
  • 37
  • 51
  • I do not wish to enumerate the instances at all. All I need to check is if SQL Server is installed or not, that is all. And thanks for the link to `sqlcmd`, might want to look into that! – GPX Aug 12 '11 at 08:05
  • I don't see the difference: if there is at least one instance, then SQL Server is installed. The path to sqlcmd.exe is in the registry, but that doesn't mean that SQL Server is installed, just that the client tools are. – Pondlife Aug 12 '11 at 10:00