SqlConnection - 是远程连接还是本地连接?

发布于 2024-07-19 19:27:09 字数 81 浏览 5 评论 0原文

如果我有 SqlConnection 对象,如何确定它是本地连接(localhost 或 127.0.0.1)还是远程连接(本地区域的其他计算机)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

季末如歌 2024-07-26 19:27:09

使用带有语句的连接询问 SQL

SELECT @@SERVERNAME

,然后验证这是否与具有 Environment.MachineName 的客户端计算机名称匹配,以 SQL 实例名称为模

Ask SQL using the connection with the statement

SELECT @@SERVERNAME

then verifiy if this match the name of the client machine with Environment.MachineName, modulo the SQL instance name

徒留西风 2024-07-26 19:27:09

您可以从 SqlConnection 对象中获取连接字符串。

string s = connection.ConnectionString;

并检查该字符串的数据源或服务器元素。

编辑:提供了代码示例。

我认为这个功能应该可以工作(无论如何都没有测试过)。

private bool CheckConnectionStringLocalOrRemote(string connectionString) {

        //Local machine
        IPHostEntry entry = Dns.GetHostByAddress("127.0.0.1");

        IPAddress[] addresses = entry.AddressList;
        String[] aliases = entry.Aliases;
        string hostName = entry.HostName;           

        if(connectionString.Contains(hostName))
            return true;



        foreach (IPAddress address in addresses) {
            if (connectionString.Contains(address.ToString())) {
                return true;
            }
        }

        foreach (string alias in aliases) {
            if (connectionString.Contains(alias))
                return true;
        }


        return false;
    }

Ps:确保在 System.Net 命名空间中添加 using 语句。

You can get the connection string out of the SqlConnection obejct.

string s = connection.ConnectionString;

and check the data source or the server element of that string.

Edit: Code sample provided.

I think this function should work (not tested anyways).

private bool CheckConnectionStringLocalOrRemote(string connectionString) {

        //Local machine
        IPHostEntry entry = Dns.GetHostByAddress("127.0.0.1");

        IPAddress[] addresses = entry.AddressList;
        String[] aliases = entry.Aliases;
        string hostName = entry.HostName;           

        if(connectionString.Contains(hostName))
            return true;



        foreach (IPAddress address in addresses) {
            if (connectionString.Contains(address.ToString())) {
                return true;
            }
        }

        foreach (string alias in aliases) {
            if (connectionString.Contains(alias))
                return true;
        }


        return false;
    }

Ps: make sure to add a using statement to System.Net namespace.

悲喜皆因你 2024-07-26 19:27:09

我知道的最简单的方法是直接检查连接字符串,看看它是否包含单词 localhost、127.0.0.1、(localhost)、“.” 或本地机器名称。 检查是否以 开头,因为它们可能是正在运行的 Sql 的本地命名实例。

您可以使用 System.Environment 库来检索当前计算机名称。 您还可以考虑使用 .Net 中的 ConnectionBuilder 库来检索数据,而无需使用完整的字符串解析。 有关详细信息,请访问此处

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

↘人皮目录ツ 2024-07-26 19:27:09

您可以检查 SqlConnection.ConnectionString 属性,看看它的 server(local) 或 . 的内容> 部分,但这不是很可靠,因为 %systemroot%\system32\drivers\etc\hosts' 和各种其他 SQL 别名,foo-srv` 很可能是本地机器。

You may check SqlConnection.ConnectionString property to see if it has something like (local) or . in its' server part, but that's not very reliable because of %systemroot%\system32\drivers\etc\hosts' and various other SQL Aliases, wherebyfoo-srv` may well be a local box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文