c# - NullReferenceException错误数据库数据到表html

标签 c# sql asp.net sql-server

我在调试程序后突出显示了这条语句。我不知道如何解决这个问题,有人可以帮助我吗?

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/

相关文章:

c# - 可以在java中使用在vb.net或c#中创建的库吗?

c# - 如何将实现公共(public)接口(interface)的集合添加在一起

c# - 插入实体不会更新键字段

sql - 根据 T-SQL 中的源表填充列数据

c# - 如何更新动态生成的 asp :image in an UpdatePanel without flickering?

c# - 可以在 C# 连接器中使用 MySQL @session 变量吗?

c# - c#中的日期时间字符串问题

PHP SQL Join 查询合并多数组中的内容

php - DELETE 查询的 SQL 语法错误

java - 框架/平台选择?