今天早上我遇到了一個大問題。我的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。
你在哪裏聲明你的讀者?您還可以從哪裏訪問以下行中的讀者?迄今爲止提供的信息不足以確定問題。 – seanzi
在我的文章上編輯。但這是總是在我的SQL崩潰之前我的服務器上工作,所以我想問題是當我再次添加這個數據庫。我錯過了什麼? – Kiwimoisi
我會遍歷代碼並查看GetData方法返回的內容。如果這返回null,然後嘗試訪問閱讀器上的.HasRows屬性,則會遇到空引用異常。 – seanzi