SqlConnection - is remote or local connection?

后端 未结 4 1331
星月不相逢
星月不相逢 2021-01-27 20:20

How can I determine is it local connection (localhost or 127.0.0.1) or is it remote connection (other machine in local area) if I have SqlConnection object?

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-27 21:19

    Easiest way that I am aware of is to check the connectionstring directly to see if it contains either the words localhost, 127.0.0.1, (localhost), "." or the local machine name. Check for starting with since their could be a local named instance of Sql running.

    You can use the System.Environment library to retrieve the current machine name. You can also look at using the ConnectionBuilder library in .Net to retrieve the data without using complete string parsing. Details on this can be found here

提交回复
热议问题