从控制台应用程序访问 SQL Server
我有一个连接到 SQL Server 数据库的简单控制台应用程序。 但是运行时会抛出以下错误。 有什么线索吗?
Unhandled Exception: System.Data.SqlClient.SqlException: Cannot open database "Database" requested by the login. The login failed.
Login failed for user 'MYDOMAIN\MYUSERID'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, BooleanignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
我使用 SQL Server 2005 Express 版。 不过,我可以使用数据库资源管理器从 Visual Web Developer 连接到 SQL Server。 我使用的代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
public class Test
{
public Test()
{
}
static void Main()
{
Console.WriteLine("hello");
string connectionString = "Data Source=localhost\\SQLEXPRESS;Database=Database;Integrated Security=true";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
Console.WriteLine("done");
}
}
I have a simple Console Application which connects to SQL Server database.
However it throws the following error while running. Any clues?
Unhandled Exception: System.Data.SqlClient.SqlException: Cannot open database "Database" requested by the login. The login failed.
Login failed for user 'MYDOMAIN\MYUSERID'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, BooleanignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
I use SQL Server 2005 Express Edition.
However I am able to connect to SQL Server from Visual Web developer using Database Explorer.
The code I used is given below:
using System;
using System.Data;
using System.Data.SqlClient;
public class Test
{
public Test()
{
}
static void Main()
{
Console.WriteLine("hello");
string connectionString = "Data Source=localhost\\SQLEXPRESS;Database=Database;Integrated Security=true";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
Console.WriteLine("done");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
控制台应用程序可以愉快地与 SQLExpress 数据库对话。 我希望您只需通过 管理工作室。 据推测,Web 开发人员应用程序正在使用 ASPNET 帐户,该帐户具有不同的域凭据。
Console apps can talk happily to SQLExpress databases. I expect you simply need to configure access to your domain account via management studio. Presumably, the web-developer app is using the ASPNET account, which has different domain credentials.
在 app.config 中验证属性“connectionstring”的密码是否正确。
我确实遇到了和你一样的问题。 而且我的密码是空的。 我不知道为什么
Verify in app.config if the password is OK on attribute "connectionstring".
I did have the same problem you had. And my password was empty. I don´t know why
这意味着您的登录无权访问该数据库。
It means your login has no rights to access that database.
我猜您可能在 Web 开发人员和您的 .net 应用程序之间使用不同的协议。
作为快速测试,您可以启动 Sql Server 配置管理器并在网络配置下启用每个协议。 您还可以在该配置工具中检查/更改 sql 本机客户端的客户端协议。 默认情况下,它们都应该启用共享内存,但仔细检查也没有什么坏处。
来自 http://msdn.microsoft.com/en-us/library/ms345154 .aspx:
我假设这是 sql server 的本地安装,而不是通过网络安装,对吗?
I would guess you may be using a different protocol between the web dev and your .net app.
As a quick test, you can launch Sql Server Configuration Manager and enable each protocol under the network configuration. You can also check/change the client protocols for sql native client in that config tool. They should both have shared memory enabled by default, but doesn't hurt to double check.
From http://msdn.microsoft.com/en-us/library/ms345154.aspx:
I'm assuming that this is a local install of sql server, not over the network right?
如果您运行的是 Vista,则可能会发生这种情况,因为 Visual Web Developer 在特权帐户(例如管理员)下运行。 这样做是为了让您能够开发和调试您的应用程序。 管理员帐户包含在 MSSQL 上的管理员组中。 您的控制台应用程序在您的帐户下运行,该帐户没有连接到 MSSQL 的权限。
If you are running Vista, then this probably happens because Visual Web Developer runs under a privileged account, e.g. Administrator. This is done to allow you to develop and debug your apps. The Administrator account is included in the admins group on MSSQL. Your console application runs under your account which does not have permissions to connect to MSSQL.