简体   繁体   中英

Error with App.config connectionstrign in class library

I have an error with connection string in class library project. In Appconfig I have this code:

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

Connection string I call form a static function:

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

When I use this connection string in function

Sqlconnection conn = new Sqlconnection(Class1.ConnDb)

I have some error like

{"Object reference not set to an instance of an object."}

What can I do?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM