【问题标题】:SQL Query Throws Exception When Run From IIS Server?从 IIS 服务器运行 SQL 查询时抛出异常?
【发布时间】:2015-06-23 19:06:15
【问题描述】:

我使用 Web 窗体构建了一个简单的 ASP.NET Web 应用程序。在默认页面上,单击按钮来运行 SQL 查询,其输出被添加到三个列表框中。源码如下:

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 WebApplication3
{
public partial class _Default : Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {


        SqlConnection myConnection = new SqlConnection("user id=WebApp;" +
                                    "password=[REMOVED];" + 
                                    "server=JC-SRV;" +
                                    "Trusted_Connection=yes;" +
                                    "database=Database; " +
                                    "connection timeout=30");

        string Command = ("SELECT *" + "FROM Table");


        try
        {
            TextBox4.Text = ("Attempting to connect to database...");
            myConnection.Open();
            TextBox4.Text = ("Success!");
            SqlCommand myCommand = new SqlCommand(Command, myConnection);
            try
            {
                SqlDataReader myReader = null;
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    ListBox1.Items.Add("Field 1 " + myReader["Test1"].ToString());
                    ListBox2.Items.Add("Field 2 " + myReader["Test2"].ToString());
                    ListBox3.Items.Add("Field 3 " + myReader["Test3"].ToString());

                }
            }

            catch (Exception ex)
            {
                TextBox4.Text = (ex.ToString());

            }

            finally
            {
                myConnection.Close();
            }


        }
        catch (Exception ex)
        {
            TextBox4.Text = (ex.ToString());

        }
    }
    }
}

当我在 Visual Studio 2013 和 IIS Express 中从我的开发机器上运行时,这可以正常工作,但是,当我将项目发布到安装了 IIS 7.0 的 Server 2008 R2 机器时,会引发以下异常:

System.Data.SqlClient.SqlException (0x80131904): 用户登录失败 '查曼\JC-SRV$'。在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 breakConnection,Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 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, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 重试,DbConnectionOptions userOptions,DbConnectionInternal& 连接)在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 重试,DbConnectionOptions userOptions)在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 重试)在 System.Data.SqlClient.SqlConnection.Open() 在 WebApplication3._Default.Button1_Click(对象发送者,EventArgs e)ClientConnectionId:0516c418-145d-4558-b9d7-ed2e55c74724

可能值得注意的是,服务器是一个名为 CHARMAN 的域的 DC,而 JC-SRV 是机器名。 SQL 2014 服务器也在这台机器上运行。

任何帮助将不胜感激。

【问题讨论】:

  • 错误非常严重..有问题的机器不允许登录数据库。
  • 当我使用 Trusted_Connection 时,我假设从我的应用程序运行的上下文中进行 Windows 身份验证。在我的情况下,它通常是我网站的应用程序池的运行用户。在这些情况下,我在连接字符串中从来没有用户名和密码。看起来你可能想要 sql 身份验证?
  • @Andre 我可以使用来自 SMSS 和同一台机器的凭据访问数据库...当应用程序位于 Web 服务器中时,这会有什么不同?
  • @JasonEades 我删除了trusted_connection,但仍然出现同样的错误。 ID 和密码用于 SQL 身份验证。你有什么建议?
  • 尝试这样的连接字符串:Data Source=server;Initial Catalog=MyDatabase;User Id=blah;Password=blah;

标签: c# asp.net sql-server iis


【解决方案1】:

感谢This Post我加了

Integrated Security=False

到连接字符串,它工作得很好

【讨论】:

    【解决方案2】:

    联系您的 DBA 并获取相关 SQL Server 实例上“CHARMAN\JC-SRV$”所需的权限。

    注意:我没有添加 cmets 的权限,因此发布我的评论作为答案。

    【讨论】:

    • 感谢您的建议,但我只是在家里玩自己的网络。因此没有人可以联系。
    • 如果您有 SA 角色,则在您需要从安全连接的数据库上向该用户提供公共角色。 (至少公共角色。)
    猜你喜欢
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多