我的数据库的连接字符串在我的项目的 Web.config 中设置:
<connectionStrings>
<add name="ServiceConnectionString"
connectionString="Data Source=localhost\sqlexpress;Initial
Catalog=Service;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
这是一个正确的配置,当我从服务器资源管理器执行存储过程时,它们按预期正确返回。
我的问题源于当我尝试通过(AddProduct 返回一个 int)这样的调用来内联访问这个数据库时:
return new serviceSQLDataContext()
.AddProduct(name, category, price, quantity);
在这条线上失败了:
base(global::System.Configuration.ConfigurationManager
.ConnectionStrings["ServiceConnectionString"]
.ConnectionString, mappingSource)
第一个参数触发“对象引用未设置为对象的实例”异常。
这怎么可能在我的 Web.config 中看到我指定了正确的连接字符串?有没有解决办法?