开发者

How can I check if the connected SQL Server is on the local machine?

开发者 https://www.devze.com 2023-01-11 02:44 出处:网络
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 remo开发者_高级运维te?

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 remo开发者_高级运维te?

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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消