需要连接字符串的帮助

本文关键字:帮助 字符串 连接 | 更新日期: 2023-09-27 18:06:09

<connectionStrings>
  <add name="mycollegeproject" connectionString="Data Source=mssql2.gear.host;Initial Catalog=mycollegeproject;Persist Security Info=True;User ID=mycollegeproject;Password=*******" />
</connectionStrings>
public static string strcon = ConfigurationManager.ConnectionStrings["mycollegeproject"].ConnectionString;

和错误

System.NullReferenceException: Object reference not set to an instance of an object.

需要连接字符串的帮助

如果没有错,大多数情况下,您没有在您的App.config中定义的连接字符串命名为mycollegeproject(或)您在代码中使用不同的键名而不是使用mycollegeproject,因此下面的代码行返回NULL导致NRE。

ConfigurationManager.ConnectionStrings["mycollegeproject"]