2

我正在使用以下代码

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\mr\\Desktop\\ment Final\\ment Final\\App_Data\\LoginStuff.mdf;Integrated Security=True;User Instance=True");

因此,每次我在新 PC 上运行项目时,我都必须一次又一次地更改所有数据源。有什么可以避免的吗?

4

3 回答 3

4

而是使用连接字符串和 web.config 来缓和更改。

web.config 示例:

<configuration>
    <connectionStrings>
        <add name="LoginDB" connectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\mr\\Desktop\\ment Final\\ment Final\\App_Data\\LoginStuff.mdf;Integrated Security=True;User Instance=True" />
    </connectionStrings>
</configuration>

将您的代码更改为:

SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["LoginDB"].ConnectionString);

您可能已经在项目的根目录中有一个 web.config,您可以简单地添加这个 connectionStrings 部分,或者如果该部分已经存在,则只需添加这个新的连接字符串!

于 2013-09-05T18:31:33.973 回答
0

To reiterate what Amit and Subterfuge said above, your connection string should be stored in a configuration file instead of in the code. Those methods won't solve the face that you would still need to modify the connection string on every machine though. Instead, I recommend you create a convention for local development and use configuration transforms between development/staging/production environments.

Local Development Convention It will be easiest to develop against your code locally if you put the database in the same location on every machine. The .NET convention is to use the app_data folder in your project's root folder. If you place your database in this folder on each machine you're developing on then you won't need to change the connection string.

For example, copy LoginStuff.mdf into your project's /app_data folder (create the folder if it doesn't exist). Change your connection string to:

<connectionString>
    <add name="loginStuff" connectionstring="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|LoginStuff.mdf;User Instance=true"
</connectionString>

Then in code you can reference it by adding a using to System.Configuration and then referencing the connection string:

var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["loginStuff"].ConnectionString);

Between Environments Between environments (debug, release, etc) you can use configuration transforms to modify the web.config automatically. For each environment you need to create a web.{environment}.config file in your project's root directory. Use XML transformations to specify how you want to modify your configuration file.

These should cover your two most common scenarios. Cheers!

于 2013-09-05T20:06:00.773 回答
0

Use config file. Can be found in root of the project as web.config in ASP.NET and app.config in class and windows project

<connectionString>
 <add 
     name="connStr" 
     connectionstring="Server=myServerAddress;
                       Database=myDataBase;
                       Trusted_Connection=True;"
 </connectionString>

In cs

SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
于 2013-09-05T18:30:47.457 回答