我在调试程序后突出显示了这条语句。我不知道如何解决这个问题,有人可以帮助我吗?
string constr = ConfigurationManager.ConnectionStrings["Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes"].ConnectionString;
这行有问题吗?我正在连接到 localdb。
最佳答案
在此行中需要来自 web.config 的该值的键名
ConfigurationManager.ConnectionStrings["Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes"].ConnectionString;
由于在 ConnectionStrings
节点中找不到任何键名 Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes
,因此抛出 NullReferenceException
尝试这样
web.config
<connectionStrings>
<add
name="myConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"/>
</connectionStrings>
C#
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
关于c# - NullReferenceException错误数据库数据到表html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292501/