簡體   English   中英

C#ASP.NET SQL Server Web.config文件

[英]c# asp.net sql server web.config file

我的應用程序無法從web.config文件中選擇連接字符串:

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.

Web.config文件

問候,Praveen N

始終記住,應用程序層配置文件始終會覆蓋DAL層配置文件。 因此,在運行應用程序時,始終將連接字符串放在應用程序層配置文件中。

謝謝Deepak 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM