【问题标题】:Error with App.config connectionstrign in class library类库中的 App.config connectionstrign 出错
【发布时间】:2017-11-03 17:08:25
【问题描述】:

类库项目中的连接字符串出错。 在 Appconfig 我有这个代码:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=DESKTOP-123\SQLEXPRESS;Initial Catalog=MyDb;User ID=sa;Password=mypassword"  providerName="System.Data.SqlClient"/>
  </connectionStrings>

我称之为静态函数的连接字符串:

public static string ConnDb {
        get {
            return ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        }
    }

当我在函数中使用这个连接字符串时

Sqlconnection conn = new Sqlconnection(Class1.ConnDb)

我有一些错误,例如

{"对象引用未设置为对象的实例。"}

我能做什么?

【问题讨论】:

  • 代码哪里出错了?您是否通过代码查看了哪个对象为您提供了空引用?
  • 库没有 app.config,运行过程会将该连接字符串放入运行项目的配置中。
  • 调用类获取连接字符串时出错
  • App.config 需要在使用该类库的项目中。试试看,如果您仍然遇到问题,请告诉我们

标签: c# sql connection-string class-library


【解决方案1】:

您可以使用 appSettings 作为替代选项。

【讨论】:

  • 如果文件不是调用应用程序的一部分,则不会工作。
  • 而且连接字符串通常不是每个用户的设置
猜你喜欢
  • 2011-08-06
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 2013-07-04
  • 2015-03-03
  • 1970-01-01
相关资源
最近更新 更多