app.Config中的ConnectionString不起作用
本文关键字:不起作用 ConnectionString 中的 Config app | 更新日期: 2023-09-27 18:10:53
在我的winform项目中,我有一个连接字符串连接到SQL。我在app.config文件中有这个连接,像这样;
<connectionStrings>
<add name="MyConnectionString" providerName="System.Data.SqlClient"
connectionString="Server=(localdb)''v11.0; Integrated Security=true; AttachDbFileName=C:''Folder''mydataBaseName.mdf;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"
/>
和我得到连接:
string config = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
//and then
using (SqlConnection conexao = new SqlConnection(config))
{
conexao.Open();
.......
.......
}
当我运行应用程序时,我得到一个错误说:"类型为'System.Data.SqlClient '的未处理异常。SqlException " occurred in System.Data.dll"
但是如果我直接从代码中调用连接字符串(不使用app.config),一切都没问题。
string config = "Server=(localdb)''v11.0; Integrated Security=true; AttachDbFileName=C:''Folder''mydataBaseName.mdf;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
有什么好主意,怎么解决这个问题?
由于莱昂诺在c#非逐字字符串文字中,如果您想在字符串中包含单个反斜杠字符,则使用''
。
在其他文件中,反斜杠不一定是特殊字符。在网络上。配置连接字符串时,你可以直接包含反斜杠,而不需要将其加倍。
或者,换句话说,一个连接字符串工作而另一个不能工作的原因是因为你确实有两个不同的连接字符串