WebPart的資料庫連接問題

來源:互聯網
上載者:User
最近開始學習.NET 2.0了,才開始就被問題困擾了....

我很喜歡.NET 2.0中的WebPart這項功能,可是,我剛剛按教程上教的在頁面上放了一個WebPartManager,然後運行,結果就報錯了:

 

“/WebSite1”應用程式中的伺服器錯誤。

在建立與伺服器的串連時出錯。在串連到 SQL Server 2005 時,在預設的設定下 SQL Server 不允許進行遠端連線可能會導致此失敗。 (provider: SQL 網路介面, error: 26 - 定位指定的伺服器/執行個體時出錯)

說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆疊追蹤資訊,以瞭解有關該錯誤以及代碼中導致錯誤的出處的詳細資料。

SQLExpress 資料庫檔案自動建立錯誤:

連接字串使用應用程式 App_Data 目錄中的資料庫位置指定了一個本地 SQL Server Express 執行個體。由於提供者確定應用程式服務資料庫不存在,因此試圖自動建立該資料庫。要成功檢查應用程式服務資料庫是否存在並自動建立應用程式服務資料庫,必須滿足下列配置要求:

  1. 如果應用程式 App_Data 目錄不存在,則 Web 服務器帳戶必須具有對應用程式目錄的讀寫存取權限。這是因為如果不存在 App_Data 目錄,Web 服務器帳戶將會自動建立它。
  2. 如果應用程式 App_Data 目錄已存在,則 Web 服務器帳戶只要求對應用程式 App_Data 目錄的讀寫存取權限。這是必要的,因為 Web 服務器帳戶將嘗試確認 SQL Server Express 資料庫已在應用程式 App_Data 目錄中。如果從 Web 服務器帳戶撤消對 App_Data 目錄的讀存取權限,提供者便無法正確地確定 SQL Server Express 資料庫是否已存在。如果提供者試圖建立已存在的資料庫的副本,就會出錯。寫存取權限是必需的,因為建立新資料庫時需要使用 Web 服務器帳戶憑據。
  3. 電腦上必須安裝 SQL Server Express。
  4. Web 服務器帳戶的進程標識必須具有本機使用者設定檔。有關如何為電腦帳戶和域帳戶建立本機使用者設定檔的詳細資料,請參見自述文檔。

源錯誤:

執行當前 Web 請求期間產生了未處理的異常。可以使用下面的異常堆疊追蹤資訊確定有關異常原因和發生位置的資訊。

堆疊追蹤:

[SqlException (0x80131904): 在建立與伺服器的串連時出錯。在串連到 SQL Server 2005 時,在預設的設定下 SQL Server 不允許進行遠端連線可能會導致此失敗。 (provider: SQL 網路介面, error: 26 - 定位指定的伺服器/執行個體時出錯)]   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +173   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +199   System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup) +1069   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +606   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +193   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +219   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +27   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +49   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +89   System.Data.SqlClient.SqlConnection.Open() +160   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +67[HttpException (0x80004005): 無法串連到 SQL Server 資料庫。]   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +123   System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89   System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +26   System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +388

版本資訊: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42

經過一番搜查,問題終於有了答案。
原來,WebPart使用個人化服務時需要在web.config檔案裡面配置一個連接字串。
好了,解決方案如下:
第一種方法是禁用個人化服務,修改WebPartManager的屬性:Personalization -> Enabled 為 False

如果需要使用個人化服務,就得使用下面這個辦法:
1. 運行 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
按照提示安裝資料庫。

2. 在web.config檔案的connectionStrings下加入一段連接字串的配置:    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" connectionString="Data Source=localhost;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=sa;Password="
        providerName="System.Data.SqlClient" />

這段配置覆蓋了預設的連接字串,而使用自訂的。

總算,問題解決了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.