c# asp.net SQL server web.配置文件

本文关键字:web 配置文件 server SQL asp net | 更新日期: 2023-09-27 18:02:35

我的应用程序无法从web中选择连接字符串。配置文件:

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

网络。配置文件

问候,Praveen N

c# asp.net SQL server web.配置文件

始终记住应用层配置文件始终覆盖DAL层配置文件。所以当你运行应用程序时,一定要把连接字符串放在应用层配置文件中。

谢谢迪帕克Joshi

如果您试图连接到本地主机中的数据库,您必须声明您的连接字符串如下:

<configuration>
  <connectionStrings>
    <add name="DBName" connectionString="Data Source=(Your Data Source);Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

数据源是项目所连接的数据服务器。如果你想要一个更精确的答案,那么请发布你的代码和需求。

<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

要将连接字符串读入代码,请使用ConfigurationManager类:

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;