回答
你可以找到它在節點<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