从控制台应用程序访问 SQL Server

发布于 2024-07-19 04:35:41 字数 1765 浏览 4 评论 0原文

我有一个连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月亮是我掰弯的 2024-07-26 04:35:41

控制台应用程序可以愉快地与 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.

内心荒芜 2024-07-26 04:35:41

在 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

肥爪爪 2024-07-26 04:35:41

这意味着您的登录无权访问该数据库。

It means your login has no rights to access that database.

静若繁花 2024-07-26 04:35:41

我猜您可能在 Web 开发人员和您的 .net 应用程序之间使用不同的协议。
作为快速测试,您可以启动 Sql Server 配置管理器并在网络配置下启用每个协议。 您还可以在该配置工具中检查/更改 sql 本机客户端的客户端协议。 默认情况下,它们都应该启用共享内存,但仔细检查也没有什么坏处。

来自 http://msdn.microsoft.com/en-us/library/ms345154 .aspx

网络支持

仅限共享内存连接类型
在本地机器上可以通过以下方式访问
SQL Server Express 的默认设置,
尽管用户可以明确地转向
在其他支持的协议上,例如
TCP/IP 和命名管道

我假设这是 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:

Networking Support

Only the shared memory connection type
on the local machine is accessible by
default for SQL Server Express,
although the user can explicitly turn
on other supported protocols such as
TCP/IP and Named Pipes

I'm assuming that this is a local install of sql server, not over the network right?

决绝 2024-07-26 04:35:41

如果您运行的是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文