简体   繁体   English

如何使C#.net中的应用程序的连接字符串独立于计算机?

[英]How to make the connection string of an application in C#.net,Computer independent?

As I transfer my application in C#.net from one computer to another, I have to change the connection string every time as the location of the Database file has changed. 当我将C#.net中的应用程序从一台计算机转移到另一台计算机时,每次数据库文件的位置更改时,我都必须更改连接字符串。

How can I can I prevent this,so that I don't have to change the connection string again and again? 如何防止这种情况发生,而不必一次又一次更改连接字符串?

When location of the database changes, something has to change somewhere. 当数据库的位置更改时,某些地方必须更改。 Back in the ODBC days, you could define a system-wide connection and specify the just the name in the connection string. 在ODBC时代,您可以定义系统范围的连接,并仅在连接字符串中指定名称。 But if the server moves you would have to change the ODBC anyway. 但是,如果服务器移动,则无论如何都必须更改ODBC。

I can think of a few solutions here. 我可以在这里想到一些解决方案。 One is that if your database runs on the local machine, use the localhost instead of the machine name. 一种是,如果您的数据库在本地计算机上运行,​​请使用localhost而不是计算机名称。

In case it is a file, create a network share and put it on that so that you use \\\\localhost\\shareName\\file.db . 如果它是一个文件,请创建一个网络共享并将其放在上面,以便您使用\\\\localhost\\shareName\\file.db

If it is a server database and could be on other machines, use a DNS name by using a host file and assign a common name so that you could do that in different networks. 如果它是服务器数据库并且可以在其他计算机上,请通过使用主机文件使用DNS名称并分配一个通用名称,以便可以在其他网络中使用。

  • If it is feasible, put your database on a network and specify the network location in the connection string; 如果可行,请将数据库放在网络上,并在连接字符串中指定网络位置;否则,请执行以下操作。

    or 要么

  • Modify the variable part(s) of your connection string using a Connection String Builder . 使用“ 连接字符串生成器”修改连接字符串的可变部分。

Use a relative path. 使用相对路径。 For most scenarios, you can just use the built-in |DataDirectory| 对于大多数情况,您只能使用内置的| DataDirectory | , which translates to the base directory you're running from (or App_Data in ASP.NET scenarios). ,它转换为您正在从中运行的基本目录(或在ASP.NET方案中为App_Data)。

If you are using a client-server database, like SQL Express, then you should also set the server name to something like .\\SQLEXPRESS - which will use the local instance. 如果使用的是客户端服务器数据库(如SQL Express),则还应将服务器名称设置为.\\SQLEXPRESS类的名称,它将使用本地实例。

This one is an old one that comes up from time to time. 这是一个不时出现的旧版本。

Visual Studio has the ability to support multiple build environments and will allow you to specify environment specific values (ie databse connection strings) so that you may test wherever you want. Visual Studio能够支持多个构建环境,并允许您指定特定于环境的值(即,数据库连接字符串),以便您可以在任何需要的地方进行测试。

Pulled this from the Gu's blog and full post may be seen here : 从Gu的博客中提取此信息,可以在此处看到完整的帖子:

It turns out you can easily automate this configuration process within the Visual Studio build environment (and do so in a way that works both within the IDE, as well as with command-line/automated builds). 事实证明,您可以在Visual Studio构建环境中轻松地自动执行此配置过程(并且可以在IDE中以及命令行/自动构建中都可以使用)。 Below are the high-level steps you take to do this. 以下是您执行此操作的高级步骤。 They work with both VS 2005 and VS 2008. 它们可与VS 2005和VS 2008一起使用。

  1. Use ASP.NET Web Application Projects (which have MSBuild based project files) 使用ASP.NET Web应用程序项目(具有基于MSBuild的项目文件)
  2. Open the VS Configuration Manager and create new "Dev", "QA", "Staging" build configurations for your project and solution 打开VS Configuration Manager并为您的项目和解决方案创建新的“ Dev”,“ QA”,“ Staging”构建配置
  3. Add new "web.config.dev", "web.config.qa", and "web.config.staging" files in your project and customize them to contain the app's mode specific configuration settings 在您的项目中添加新的“ web.config.dev”,“ web.config.qa”和“ web.config.staging”文件,并对其进行自定义以包含应用程序特定于模式的配置设置
  4. Add a new "pre-build event" command to your project file that can automatically copy over the web.config file in your project with the appropriate mode specific version each time you build the project (for example: if your solution was in the "Dev" configuration, it would copy the web.config.dev settings to the main web.config file). 在您的项目文件中添加一个新的“ pre-build event”命令,该命令可以在每次构建项目时自动以适当的模式特定版本复制到项目中的web.config文件中(例如:如果解决方案位于“开发”配置,它将web.config.dev设置复制到主web.config文件中)。

In VS 2010 I believe most of the setup for this is done for you when you make your new application, but the principles are still the same. 我相信在VS 2010中,大部分的设置工作都是在创建新应用程序时完成的,但原理仍然相同。 You can skip the pre-build event in favor of pulling the connection string in via code if you're comfortable with that way of doing things. 如果您愿意这样做,则可以跳过预构建事件,而希望通过代码拉入连接字符串。 There is an example of this on StackOverflow here on this answered question 在这个已回答的问题上,有一个关于StackOverflow的示例

The start of the answer (or at least how I'm more likely to implement this) is to store each connection string value in a separate connections.config file with a name unique to the environment. 答案的开始(或者至少是我更可能实现此目的的方法)是将每个连接字符串值存储在单独的connections.config文件中,该文件的名称对于环境而言是唯一的。 This will only contain the values from connectionStrings portion of the web.config file. 这将仅包含来自web.config文件的connectionStrings部分的值。 I like doing this because it means adding a new environment doesn't mean a developer touching the main web.config file and you can also do some server-side trickery to remove the database connection strings from the site's webroot making it a bit more secure. 我喜欢这样做,因为这意味着添加新环境并不意味着开发人员可以触摸主要的web.config文件,而且您还可以做一些服务器端的技巧,以从站点的webroot中删除数据库连接字符串,从而使其更加安全。

Method for removing the connectionStrings to another fole may be seen here: http://www.bigresource.com/Tracker/Track-ms_sql-cberGbNT/ 可以在此处看到用于将connectionStrings删除到另一个错误的方法: http : //www.bigresource.com/Tracker/Track-ms_sql-cberGbNT/

I would also look into having a network accessible version of the database instead of making each developer have one in their environment. 我还将研究拥有可通过网络访问的数据库版本,而不是让每个开发人员在自己的环境中都拥有一个。 If you have to have disconnected work being done that makes sense, but from a management standpoint there should really only be one database for each stage of the project (Development, Staging, and Production). 如果您必须断开已完成的工作,这是有道理的,但是从管理的角度来看,对于项目的每个阶段(开发,暂存和生产),实际上应该只有一个数据库。

Hope that's helpful, and feel free to ask follow-up questions on anything that doesn't make sense. 希望对您有所帮助,并可以在没有任何意义的地方随意提出后续问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM