System.Configuration.ConfigurationManager.ConnectionStrings没有配置连接字符串 [英] System.Configuration.ConfigurationManager.ConnectionStrings has no connection string configured

查看:1870
本文介绍了System.Configuration.ConfigurationManager.ConnectionStrings没有配置连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试在我的C#代码运行任何东西,我得到以下错误:

Whenever I try to run anything in my C# code I get the following error:

System.InvalidOperationException was unhandled by user code
Message=No connection string configured

if (System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"] == null)
{
     throw new System.InvalidOperationException("No connection string configured");
}

connectionString = string.Format("{0};Application Name={1}", System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"].ConnectionString, this.applicationName);

因此 System.Configuration.ConfigurationManager.ConnectionStrings [DBContext] 为null。我真的找不到任何事情,一个可能相关的问题:如何修复ConnectionString属性尚未初始化表明某些可能是配置文件错误。在这一刻,我害怕我不知何故不小心删除了一个配置文件。

So System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"] is null. I can't really find anything about it, one question that might be related: How to fix "The ConnectionString property has not been initialized" suggests that something might be wrong with the config file. At this moment I'm afraid I somehow accidentally deleted a config file.

此外,只需在文档

Returns a ConnectionStringSettingsCollection object that contains the contents of the ConnectionStringsSection object for the current application's default configuration. 

它包含默认值,所以我倾向于它在一个配置文件,我必须不小心删除。

It contains the default values, so I'm inclined it's in a config file that I must have accidentally removed.

如果我是对的,我不知道哪一个或它应该在哪里,以及它应该包含什么。如果我错了,我不知道它从哪里来。
那么System.Configuration.ConfigurationManager.ConnectionStrings [DBContext]设置在哪里呢?

If I'm right, I don't know which one or where it should be, and what it should contain. If I'm wrong, I have no clue where it comes from. So where is the System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"] set? And/or how can I fix this issue?

推荐答案

.NET中的连接字符串来自配置文件 - app.config对于控制台/ Windows应用程序或web.config为Web应用程序。

Connection strings in .NET come from a config file - either app.config for console / Windows apps or web.config for a Web app.

您可以通过右键单击项目并添加正确的文件,并确保您在该配置文件中有以下部分:

You can add a new config file to your project by right-clicking the project and adding the correct file, and make sure you have the following section in that config file:

<configuration>
  <connectionStrings>
    <add name="DBContext" connectionString="data source=.\SQLEXPRESS;Initial Catalog=databasename;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>
  ...
</configuration>

当然,您需要将连接字符串替换为适合您的环境例如,将databasename替换为您的数据库名称)。

And of course you'd need to replace the connection string with something that is appropriate for your environment (e.g. replace databasename with your database name).

这篇关于System.Configuration.ConfigurationManager.ConnectionStrings没有配置连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆