回答
你可以找到它在节点<connectionStrings></<connectionStrings>>
例子:
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
而如何在代码阅读:
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
System.Configuration.ConnectionStringSettings connString;
if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
{
connString =
rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
if (connString != null)
Console.WriteLine("Northwind connection string = \"{0}\"",
connString.ConnectionString);
else
Console.WriteLine("No Northwind connection string");
}
更新:你需要在你的DbContext
public class YourDbContext : DbContext
{
public YourDbContext()
: base("YourConnectionName")
{
}
}
和Web配置指定的连接名称:
<connectionStrings>
<add name="YourConnectionName"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
谢谢。但我想要一个连接字符串web.config连接到DB_ALThez.mdf。请参阅上面的图像。没有服务器名称,pass.user –
ConnectionStrings元素将仅在您使用单个用户帐户时存在,如果您不使用验证,它将不存在。 –
我已经更新了我的答案。您需要创建新的连接并在您的DbContext中指定它 –
演示
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
谢谢。但我想要一个连接字符串web.config连接到DB_ALThez.mdf。看到上面的图片。没有servername,pass.user –
- 1. 通过SQL Server连接到主数据库的连接字符串是什么
- 2. 什么是SQL Server连接字符串中的“连接超时”?
- 3. 什么是连接字符串Ironpython连接到SQL Server CE?
- 4. 连接sql server和asp.net的连接字符串是什么?
- 5. SQL Server数据库连接字符串
- 6. SQL Server连接字符串尝试连接到错误的数据库
- 7. 什么是通过jdbc连接到sql server数据库的正确连接字符串
- 8. ASP.NET连接字符串到本地SQL Server数据库,使用web.config
- 9. 什么是SQL Server连接字符串中的“复制”键?
- 10. SQL Server连接字符串中“Initial Catalog”的含义是什么?
- 11. 什么是连接到SQL Server Express 2014数据库的术语?
- 12. 用于在Bot Framework的web.config文件中连接Azure SQL Server数据库的连接字符串
- 13. SQL Server连接字符串
- 14. SQL Server连接字符串
- 15. SQL Server连接字符串
- 16. SQL Server连接字符串
- 17. 什么是oledb连接连接DSN的连接字符串
- 18. web.config连接字符串到不同的数据库服务器
- 19. 打开连接,连接字符串,我的数据库名称是什么?
- 20. 用连字符名连接到Sql Server数据库
- 21. 什么是SQL Server的正确连接字符串?
- 22. SQL Server 2012默认实例的连接字符串是什么?
- 23. 用于远程连接到SQL Server 2008的连接字符串
- 24. 加密ASP.Net中的数据库连接字符串Web.config
- 25. C#winapp与SQL Server数据库文件的连接字符串
- 26. 连接到数据库(ADO.NET,SQL Server)的
- 27. 在asp.net中创建连接字符串/连接到数据库?
- 28. SQL Server Express 2008连接字符串不会出现在web.config中
- 29. web.config中缺少SQL Server Express 2008连接字符串
- 30. 在web.config中连接到MSSQL时没有连接字符串
看看这个:HTTPS://www.connectionstrings.com/ sql-server/ – TheGameiswar