[英]How to detect whether the SQL Server edition is Express?
I have a really simple question here, but I just can't find an answer that solves it. 我在这里有一个非常简单的问题,但是我找不到解决问题的答案。
I have a connection string like this: 我有这样的连接字符串:
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\DB.mdf;Initial Catalog=DB;Integrated Security=True");
It works, but when I try to use it in a PC that doesn't have SQL Server Express installed, it just won't connect. 它可以工作,但是当我尝试在未安装SQL Server Express的PC中使用它时,它将无法连接。 It'll obviously work if I remove that "\\SQLEXPRESS" piece, I know, but I'd prefer not to do it manually on each PC I install the software.
我知道,如果删除该“ \\ SQLEXPRESS”片段,显然可以使用,但是我不希望在安装该软件的每台PC上都不要手动进行操作。 I tried to do it by using try-catch, but it's too slow and I think it isn't very recommended.
我尝试使用try-catch做到这一点,但是它太慢了,我不推荐这样做。
Well, I just want to know how to do it via code. 好吧,我只想知道如何通过代码来做到这一点。 Is there a simple way to do so?
有一个简单的方法吗? Will it solve the problem completely?
它会完全解决问题吗? (I mean, will it certainly work in other editions?)
(我是说,它肯定会在其他版本中工作吗?)
Thanks in advance! 提前致谢! (And sorry for my English)
(对不起,我的英语)
Connection strings should not be hard-coded into your software. 连接字符串不应硬编码到您的软件中。
You should place them in configuration ( web.config
or app.config
) for the exact reason you are posting this question. 出于发布此问题的确切原因,应将它们放置在配置中(
web.config
或app.config
)。
You can't assume there will be any version of SQL installed on the machine that your application will run on - the user may want to use a centralized server. 您不能假定应用程序将在其上运行的计算机上安装任何版本的SQL-用户可能要使用集中式服务器。
Just to clarify: A SQL server can be installed as a 'default' or a 'named' instance. 需要说明的是:SQL Server可以作为“默认”或“命名”实例安装。 I think by default, express installs as a named instance
我认为默认情况下,快速安装作为命名实例
This allows a single server machine to run multiple SQL servers 这允许一台服务器计算机运行多个SQL服务器
To connect to a default instance you specify the server name or IP and that's all 要连接到默认实例,请指定服务器名称或IP,仅此而已
Server=SomeServer
To connect to a named instance you specify the server/ip + instance name separated by a backslash. 要连接到命名实例,请指定服务器/ ip +实例名称,并用反斜杠分隔。
Server=SomeServer\SomeInstance
In your case you are looking to connect to a SQL instance called 'SQLEXPRESS' on the local machine. 在您的情况下,您希望连接到本地计算机上名为“ SQLEXPRESS”的SQL实例。 (a dot . means local machine)
(一个点。表示本地计算机)
You ideally want to install SQL on a server machine and use an application configuration file (App.config) to specify the connection string.. 理想情况下,您希望在服务器计算机上安装SQL并使用应用程序配置文件(App.config)指定连接字符串。
App.config supports a section called ConnectionStrings which you can add your connection string to. App.config支持一个名为ConnectionStrings的部分,您可以在其中添加连接字符串。 Then if you add a reference to System.Configuration to your project you can get hold of the connectionstring using the config manager class:
然后,如果将对System.Configuration的引用添加到项目中,则可以使用config manager类来获取连接字符串:
App.config: App.config中:
<connectionStrings>
<add name="YourConnectionString"
connectionString="Server=someSqlServer\SomeInstance;Initial Catalog=SomeDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Use in code: 在代码中使用:
ConfigurationManager.ConnectionStrings["YourConnectionStringName"]
In T-SQL itself, you can inspect the edition
server property: 在T-SQL本身中,您可以检查
edition
服务器属性:
SELECT
SERVERPROPERTY('edition') as 'Product Edition'
This will return something like Express Edition (64-bit)
if the version you're checking is indeed an Express edition. 如果您要检查的版本确实是Express版本,它将返回类似
Express Edition (64-bit)
的内容。
You can read more about all other available server properties in the relevant MSDN documentation here . 您可以在相关的MSDN文档中阅读有关所有其他可用服务器属性的更多信息。
You should probably write something that lets your user choose the server. 您可能应该编写一些内容,让您的用户选择服务器。 You can use the registry to detect SQL Server instances running locally, but that won't work if you are connecting to a network instance.
您可以使用注册表来检测本地运行的SQL Server实例,但是如果您要连接到网络实例,则该方法将无效。 See this answer about using SMO (Server Management Objects).
请参阅有关使用SMO(服务器管理对象)的答案。 Programmatically detect SQL Server Edition
以编程方式检测SQL Server Edition
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.