我正在尝试在我的web.config文件(Visual Studio 2008/ASP.NET 3.5)中设置连接字符串,以连接到本地服务器(SQL server 2008)。
在我的web.config文件中,我应该在哪里放置连接字符串?如何放置?
这是我的web.config文件目前的样子:http://imwired.net/aspnet/Online_web.config
我正在尝试在我的web.config文件(Visual Studio 2008/ASP.NET 3.5)中设置连接字符串,以连接到本地服务器(SQL server 2008)。
在我的web.config文件中,我应该在哪里放置连接字符串?如何放置?
这是我的web.config文件目前的样子:http://imwired.net/aspnet/Online_web.config
<connectionStrings>
<add name="conString" connectionString="Data Source=SQLServerAddress;Initial Catalog=YourDatabaseName; User Id=SQLServerLoginId; Password=SQLServerPassword"/>
</connectionStrings>
尝试使用这个连接字符串。
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;
我刚发现!! 你需要将这个字符串connection直接指向你的数据库。 服务器上情况也是如此。
"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=c:/inetpub/wwwroot/TEST/data/data.mdb;"
它运行了!:)
将连接字符串存储在web.config文件中
将应用程序的连接字符串存储在配置文件中而不是硬编码到代码中是一种良好的实践。在.NET 2.0和.NET 3.5(及以上版本)之间的操作方式有所不同。本文涵盖了两者。 https://www.connectionstrings.com/store-connection-string-in-webconfig/
web.config
文件中创建一个名为 <connectionStrings></connectionStrings>
的部分,并将不同的连接字符串添加到其中,例如: <configuration></configuration>
。<configuration>
<connectionStrings>
<add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>
</connectionStrings>
</configuration>
这里是所有不同连接字符串格式的列表https://msdn.microsoft.com/zh-cn/library/jj653752(v=vs.110).aspx