如何在安装的SQL Server(标准版)打通code服务器名称 [英] How to get server name through code if SQL Server (Standard Edition) is installed

查看:118
本文介绍了如何在安装的SQL Server(标准版)打通code服务器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过code得到,如果是安装了SQL Server(标准版)的服务器名称。

我们通过服务器名称,同时创建一个连接字符串来连接的SQL Server。我们可以检索该价值code?

 字符串sqlConnectionString =的String.Format(
用户ID = {0};密码= {1};服务器= {2}; Trusted_Connection =无;数据库= TestDB的;
连接超时= {3},
dirDBinfo.UserName,dirDBinfo.Password,服务器名称,dirDBinfo.TimeOut);
 

解决方案

我不知道我知道你想要什么。

如果您已经有一个连接字符串,而你正试图从中提取的服务器名称用在其他地方,你可以反向工程,像这样:

  VAR分析器=新SqlConnectionStringBuilder(的connectionString);
VAR的serverName = parser.DataSource;
 

如果您正在构建连接字符串的第一次,那么:

  1. 如果您知道要连接到机器上的SQL Server的客户端code被执行,然后只需使用(本地)的服务器名称。如果SQL Server有一个实例名称,然后指定它是这样的:(本地)\ myinstancename
  2. 如果你不事先知道要连接的服务器,那么它是由你来从其他地方获得该信息。

How to get server name through code if SQL Server (Standard Edition) is installed.

We pass the server name while creating a connection string to connect SQL Server. Can we retrieve this value through code?

string sqlConnectionString = string.Format(
"user id={0};password={1};server={2};Trusted_Connection=no;database=TestDB;
connection timeout={3}",
dirDBinfo.UserName, dirDBinfo.Password, "ServerName", dirDBinfo.TimeOut);

解决方案

I'm not sure I understand what you want.

If you already have a connection string, and you are trying to extract the server name from it for use elsewhere, you can reverse-engineer it like so:

var parser = new SqlConnectionStringBuilder(connectionString);
var serverName = parser.DataSource;

If you are constructing your connection string for the first time, then:

  1. If you know that you want to connect to the SQL Server on the machine that your client code is executing on, then just use (local) for the server name. If the SQL Server has an instance name, then specify it like this: (local)\myinstancename.
  2. If you don't know in advance what server to connect to, then it's up to you to obtain that information from somewhere else.

这篇关于如何在安装的SQL Server(标准版)打通code服务器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆