连接到数据库
今天早上我遇到了一个大问题。我的 SQL Server 昨天有一个巨大的错误,我不得不重新安装它。
在我的客户端,当我尝试登录并输入密码时,我总是收到此错误:
未将对象引用设置为对象的实例。
描述:执行期间发生未处理的异常 当前的网络请求。请检查堆栈跟踪 有关错误及其起源的更多信息 代码。
异常详细信息:System.NullReferenceException:对象引用不是 设置为对象的实例。
来源错误:
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>
我还设置了具有数据读取和数据写入访问权限的 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 。
I got a big problem this morning. My SQL Server had a HUGE bug yesterday and I had to reinstall it.
On my client side, when I try to login, and put the password also i always receive this error:
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();
It's like nothing is coming back from my sql database... That's my connection string . I really don't know what's wrong with it .
<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>
I also set the clientportaluser with dataread and datawrite access.
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;
FIXED : The error was users instance set to false .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你有两个“Integrated Security=SSPI;”和
“用户 ID=clientportaluser;密码=aspen1aspen1;”在一起
使用一个然后尝试。
Why you have both "Integrated Security=SSPI;" and
"User Id=clientportaluser;Password=aspen1aspen1;" toghether
use one and then try.
如果您在连接时收到错误(如果生成了异常,请发布异常),那么很可能是在恢复数据库时,您没有将用户帐户添加回有效用户。请检查您的用户是否有权访问数据库,并且有足够的权限来执行/选择您尝试执行的任何操作。
如果您使用的是 Sql Server Management Studio,请查看安全分支。
If you are getting the error onconnect (please post the exception if one is generated) then it's likely that when you restored your database, you didn't add your user account back as a valid user. Please check that your user is entitled to access the database, and that it has sufficient permissions to execute/select whatever you are trying to do.
Look at the Security branch if you are using Sql Server Management Studio.
如果您启动 SQL Profiler,是否有任何内容被发送到数据库?您可以通过 Management Studio 访问数据库吗?
If you fire up SQL Profiler is anything being sent to the database? Can you access the database via Management Studio?