当两台计算机上的SQL Server Express实例不同时,在connectionstring中定义数据源

时间:2022-06-12 17:02:47

I work on an ASP.NET MVC solution with my colleague. Each one works on his own computer. My instance of SQL Server Express is called SQLEXPRESS, while his instance is the default (local) instance.

我和我的同事一起研究ASP.NET MVC解决方案。每个人都在自己的电脑上工作。我的SQL Server Express实例称为SQLEXPRESS,而他的实例是默认(本地)实例。

My connection string in web.config is like this:

我在web.config中的连接字符串是这样的:

<add name="Prog.Models.BreezeContext" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Prog;Integrated Security=True"/>

At this time, my colleague needs to change the connection string to this:

这时,我的同事需要将连接字符串更改为:

<add name="Prog.Models.BreezeContext" providerName="System.Data.SqlClient" connectionString="Data Source=(local);Initial Catalog=Prog;Integrated Security=True"/>

You will notice the difference in the datasource: .\SQLEXPRESS <> (local)

您会注意到数据源的不同之处:。\ SQLEXPRESS <>(本地)

My question: how can we proceed to be able to avoid changing the connection string each time we run the app? I don't want to uninstall/reinstall my SQL Server Express to have a local instance. I wonder if there is another option.

我的问题:我们怎样才能避免每次运行应用程序时都改变连接字符串?我不想卸载/重新安装我的SQL Server Express以拥有本地实例。我想知道是否还有其他选择。

Thanks.

谢谢。

1 个解决方案

#1


1  

You can create two config files which are specific to each machine and use a pre-build script to determine which one to use.

您可以创建两个特定于每台计算机的配置文件,并使用预构建脚本来确定要使用哪个。

e.g. you can have two connectionstrings.config files, connectionstrings.bronzato.config and connectionstrings.colleague.config, each with the environment specific settings.

例如你可以有两个connectionstrings.config文件,connectionstrings.bronzato.config和connectionstrings.colleague.config,每个都有特定于环境的设置。

In the pre-build script you can look up the machine name environment variable and copy your file over connectionstrings.config. This way you can have multiple config files and the system will use the correct one depending on the environment.

在预构建脚本中,您可以查找计算机名称环境变量,并通过connectionstrings.config复制文件。这样您就可以拥有多个配置文件,系统将根据环境使用正确的配置文件。

You can also create a separate configuration setting and use that to determine which file to use in the pre-build script.

您还可以创建单独的配置设置,并使用它来确定在预构建脚本中使用哪个文件。

#1


1  

You can create two config files which are specific to each machine and use a pre-build script to determine which one to use.

您可以创建两个特定于每台计算机的配置文件,并使用预构建脚本来确定要使用哪个。

e.g. you can have two connectionstrings.config files, connectionstrings.bronzato.config and connectionstrings.colleague.config, each with the environment specific settings.

例如你可以有两个connectionstrings.config文件,connectionstrings.bronzato.config和connectionstrings.colleague.config,每个都有特定于环境的设置。

In the pre-build script you can look up the machine name environment variable and copy your file over connectionstrings.config. This way you can have multiple config files and the system will use the correct one depending on the environment.

在预构建脚本中,您可以查找计算机名称环境变量,并通过connectionstrings.config复制文件。这样您就可以拥有多个配置文件,系统将根据环境使用正确的配置文件。

You can also create a separate configuration setting and use that to determine which file to use in the pre-build script.

您还可以创建单独的配置设置,并使用它来确定在预构建脚本中使用哪个文件。