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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/828957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:17:08  来源:igfitidea点击:

Accessing SQL Server from Console application

c#sql-serverconsole-application

提问by Rejeev Divakaran

I have a simple Console Application which connects to SQL Server database. However it throws the following error while running. Any clues?

我有一个连接到 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)

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:

我使用 SQL Server 2005 速成版。但是,我可以使用数据库资源管理器从 Visual Web 开发人员连接到 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");
        }
    }

回答by Marc Gravell

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.

控制台应用程序可以愉快地与 SQLExpress 数据库对话。我希望您只需要通过management studio配置对您的域帐户的访问。据推测,Web 开发人员应用程序正在使用 ASPNET 帐户,该帐户具有不同的域凭据。

回答by ssvinarenko

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.

如果您运行的是 Vista,那么这可能是因为 Visual Web Developer 在特权帐户下运行,例如管理员。这样做是为了允许您开发和调试您的应用程序。管理员帐户包含在 MSSQL 的 admins 组中。您的控制台应用程序在您无权连接到 MSSQL 的帐户下运行。

回答by ahains

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.

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

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

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

网络支持

默认情况下,SQL Server Express 只能访问本地计算机上的共享内存连接类型,尽管用户可以显式打开其他支持的协议,例如 TCP/IP 和命名管道

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

我假设这是 sql server 的本地安装,而不是通过网络安装,对吗?

回答by Pablo S G Pacheco

Verify in app.config if the password is OK on attribute "connectionstring".

在 app.config 中验证属性“connectionstring”上的密码是否正确。

I did have the same problem you had. And my password was empty. I don′t know why

我确实遇到了和你一样的问题。而我的密码是空的。我不知道为什么

回答by binier

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

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