在IIS服务器上运行时,SQL查询抛出异常?

18 浏览
0 Comments

在IIS服务器上运行时,SQL查询抛出异常?

我使用Web Forms构建了一个简单的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=[已删除];" +

"server=JC-SRV;" +

"Trusted_Connection=yes;" +

"database=Database; " +

"connection timeout=30");

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

try

{

TextBox4.Text = ("正在尝试连接到数据库...");

myConnection.Open();

TextBox4.Text = ("成功!");

SqlCommand myCommand = new SqlCommand(Command, myConnection);

try

{

SqlDataReader myReader = null;

myReader = myCommand.ExecuteReader();

while (myReader.Read())

{

ListBox1.Items.Add("字段1 " + myReader["Test1"].ToString());

ListBox2.Items.Add("字段2 " + myReader["Test2"].ToString());

ListBox3.Items.Add("字段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): 登录失败,用户'CHARMAN\JC-SRV$'。在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 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, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() at WebApplication3._Default.Button1_Click(Object sender, EventArgs e)ClientConnectionId:0516c418-145d-4558-b9d7-ed2e55c74724

值得注意的是,该服务器是一个名为CHARMAN的域的DC,JC-SRV是计算机名。SQL 2014服务器也在该计算机上运行。

非常感谢您的帮助。

0
0 Comments

问题的出现的原因是SQL查询在IIS服务器上运行时抛出异常。解决方法是联系数据库管理员(DBA)并获取"CHARMAN\JC-SRV$"在相关的SQL Server实例上所需的权限。如果你拥有SA角色,则在安全性中为此用户提供需要连接的数据库的公共角色(至少是公共角色)。

0
0 Comments

问题的原因:在IIS服务器上运行SQL查询时,会抛出异常。

解决方法:根据这个帖子中的建议,向连接字符串中添加Integrated Security=False,问题得到解决。

0