views:

70

answers:

2

Is there any way of telling, using C#, if the Sql Server I'm connected to in Ado.Net is on the local machine rather than remote?

I'm wanting to know whether I have access to the file system where SQL Server stores, for example it's backup files. That would let me determine whether I would be able to delete a backup file programatically if I get SQL Server to create one. I have read about xp_cmdshell and such like, but I believe it's best to avoid them.

A: 

Just check if System.IO.File.Exists = True. Even if the files are on a network, the user may have permission (Or be given permission).

Jeff O
+2  A: 

Use SERVERPROPERTY('MachineName'):

Windows computer name on which the server instance is running.

For a clustered instance, an instance of SQL Server running on a virtual server on Microsoft Cluster Service, it returns the name of the virtual server.

You should not use HOST_NAME() because that returns the name of the workstation, in other words the name of the client, and is always going to appear that you're connected locally.

Remus Rusanu
I've deleted my answer because it is wrong and I don't want other developers to come here and pick up something incorrect by mistake. I didn't look hard enough when I was testing this earlier. Remus' answer is the correct one. If it's possible Samuel, you should mark this as the official answer.
Joel Goodwin