Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET服务器错误:用户登录失败'';_C#_Asp.net_Ado.net - Fatal编程技术网

C# ASP.NET服务器错误:用户登录失败'';

C# ASP.NET服务器错误:用户登录失败'';,c#,asp.net,ado.net,C#,Asp.net,Ado.net,我正在尝试在我的应用程序和sql server 2008之间建立连接。但是,我无法连接,因为我面临以下错误 Server Error in '/' Application. Login failed for user ''. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more in

我正在尝试在我的应用程序和sql server 2008之间建立连接。但是,我无法连接,因为我面临以下错误

Server Error in '/' Application.

Login failed for user ''.

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.Data.SqlClient.SqlException: Login failed for user ''.

Source Error: 


Line 15:             SqlConnection con = new SqlConnection("data source = SEKHAR-PC;database = MyDB, integrated security = SSPI");
Line 16:             SqlCommand cmd = new SqlCommand("Select * from EmployeeTable", con);
Line 17:             con.Open();
Line 18:             SqlDataReader rdr = cmd.ExecuteReader();
Line 19:             GridView1.DataSource = rdr;

Source File: C:\Users\Hp\documents\visual studio 2010\Projects\CSProject\CSProject\ADO1.aspx.cs    Line: 17 

Stack Trace: 


[SqlException (0x80131904): Login failed for user ''.]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009598
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +35
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +183
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +239
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +195
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +232
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +185
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +33
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +524
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
   System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +479
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +108
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
   System.Data.SqlClient.SqlConnection.Open() +125
   CSProject.ADO1.Page_Load(Object sender, EventArgs e) in C:\Users\Hp\documents\visual studio 2010\Projects\CSProject\CSProject\ADO1.aspx.cs:17
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225
下面是我的代码:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CSProject
{
    public partial class ADO1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source = SEKHAR-PC;database = MyDB, integrated security = SSPI");
            SqlCommand cmd = new SqlCommand("Select * from EmployeeTable", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            GridView1.DataSource = rdr;
            GridView1.DataBind();
            con.Close();
        }

    }
}

我正在使用windows身份验证。服务器名=SEKHAR-PC。您能帮我一下吗?我是ADO.NET新手,不知道如何处理此问题。

我认为问题在于您的连接字符串。当您尝试使用windows身份验证模式访问sql server时,连接字符串应如下所示-

SqlConnection con = new SqlConnection("data source = SEKHAR-PC; initial catalog= MyDB, integrated security = true");

请通过在web.config中提供模拟来提供用户名和密码

<system.web>
        <identity impersonate="true" userName="domain\username" password="********"/>
    </system.web>

详情请参阅