2011-12-01 130 views
0

今天早上我遇到了一個大問題。我的SQL Server昨天有一個巨大的錯誤,我不得不重新安裝它。連接數據庫

在我的客戶端,當我嘗試登錄,並把密碼也,我總是收到此錯誤:

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.

Source Error:

public User GetUser(string username, string password) 
    { 
     // Get the User 

     dynamic queryStringLogon = ("SELECT Login, Id, Password, BugLogin, Guid FROM Users WHERE (Login LIKE '" + username + "') "); 

     SqlCommand theCommand = new SqlCommand(queryStringLogon); 

     SqlDataReader reader = GetData(theCommand); 

Line 31:    User user = null; 
Line 32: 
Line 33:    if (reader.HasRows) 
Line 34:    { 
Line 35:    user = new User(); 

這像什麼就要從我的SQL數據庫返回......這就是我的連接字符串。我真的不知道它有什麼問題。

<connectionStrings> 
    <remove name="LocalSqlServer" /> 
    <add name="LocalSqlServer" 
     connectionString="data source=.\SQLEXPRESS;Initial Catalog=DataUi;Integrated Security=SSPI;User Id=clientportaluser;Password=aspen1aspen1;User Instance=true" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 

我還設置有DATAREAD和datawrite訪問clientportaluser。

private SqlDataReader GetData(SqlCommand command) 
    { 
     command.Connection = Connection; 

     //using (Connection) 
     //{ 

     try 
     { 

      if (Connection.State == System.Data.ConnectionState.Closed) 
      { 
       Connection.Open(); 
      } 

      SqlDataReader reader = command.ExecuteReader(); 
      return reader; 
     } 
     catch (Exception e) 
     { 
      return null; 
     } 
     finally 
     { 
      // Connection.Close(); 
     } 

     //} 
    } 

    private SqlConnection Connection 
    { 
     get 
     { 
      if (_sqlConnection == null) 
      { 
       _sqlConnection = new SqlConnection(_conString); 
      } 
      return _sqlConnection; 
     } 
    } 

    private string _conString; 

    private SqlConnection _sqlConnection; 

修復:錯誤是用戶實例設置爲false。

+0

你在哪裏聲明你的讀者?您還可以從哪裏訪問以下行中的讀者?迄今爲止提供的信息不足以確定問題。 – seanzi

+0

在我的文章上編輯。但這是總是在我的SQL崩潰之前我的服務器上工作,所以我想問題是當我再次添加這個數據庫。我錯過了什麼? – Kiwimoisi

+0

我會遍歷代碼並查看GetData方法返回的內容。如果這返回null,然後嘗試訪問閱讀器上的.HasRows屬性,則會遇到空引用異常。 – seanzi

回答

1

如果您收到錯誤onconnect(請在生成異常時發佈異常),那麼很可能當您恢復數據庫時,您沒有將您的用戶帳戶添加爲有效用戶。請檢查您的用戶是否有權訪問數據庫,並且它有足夠的權限執行/選擇您正在嘗試執行的任何操作。

如果您使用的是Sql Server Management Studio,請查看Security分支。

+0

現在我總是得到這個錯誤.. 錯誤:15372嚴重性:16,狀態:1 2011-12-01 16:12:08.52登錄由於啓動用戶實例的進程失敗,無法生成SQL Server的用戶實例。連接將被關閉。 [客戶端:<本地計算機>] – Kiwimoisi

+1

刪除用戶實例部分。看到http://stackoverflow.com/questions/4770596/failed-to-generate-a-user-instance-of-sql-server-due-to-failure-in-retrieving-th – dash

2

爲什麼你既「集成安全= SSPI;」和 「User Id = clientportaluser; Password = aspen1aspen1;」以 使用一個,然後嘗試。

+0

我嘗試了兩個,不改變什麼。 – Kiwimoisi

1

如果啓動SQL事件探查器是將任何事情發送到數據庫?你可以通過Management Studio訪問數據庫嗎?

+0

我用示蹤劑看什麼發生.. – Kiwimoisi

+0

錯誤:15372嚴重性:16,狀態:1 2011-12-01 16:12:08.52登錄由於未能啓動用戶實例的進程,無法生成SQL Server的用戶實例。連接將被關閉。 [客戶端:<本地計算機>] – Kiwimoisi

+1

這是否也在sql服務器的錯誤日誌中?如果是這樣,這聽起來像SQL服務器沒有正確重新安裝。如果探查器沒有連接,那麼你可以排除你的應用程序 – Paul