C# 从IIS服务器运行SQL查询时引发异常?

C# 从IIS服务器运行SQL查询时引发异常?,c#,asp.net,sql-server,iis,C#,Asp.net,Sql Server,Iis,我使用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; namespac

我使用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 box时,会引发以下异常:

System.Data.SqlClient.SqlException 0x80131904:用户登录失败 “CHARMAN\JC-SRV$”。在 System.Data.SqlClient.SqlInternalConnection.OnErrorSqlException 异常,布尔断开连接,Action1 wrapCloseInAction位于 System.Data.SqlClient.TDSpaser.ThroweException和WarningDSParserStateObject stateObj,布尔调用方连接锁,布尔异步关闭 System.Data.SqlClient.TdsParser.tryrunbenhavior运行行为, SqlCommand cmdHandler、SqlDataReader数据流、, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj、Boolean和dataReady位于 System.Data.SqlClient.TdsParser.runBehavior, SqlCommand cmdHandler、SqlDataReader数据流、, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj at System.Data.SqlClient.SqlInternalConnectionDS.CompleteLoginBoolean 登记在 System.Data.SqlClient.SqlInternalConnectionDS.AttemptOneLoginServerInfo serverInfo、String newPassword、SecureString newSecurePassword、, 布尔值IgnoresInOpenTimeout,TimeoutTimer timeout,布尔值 故障转移在 System.Data.SqlClient.SqlInternalConnectionDs.LoginNoFailoverServerInfo serverInfo、String newPassword、SecureString newSecurePassword、, Boolean redirectedUserInstance,SqlConnectionString connectionOptions, SqlCredential凭据,超时时间为 System.Data.SqlClient.SqlInternalConnectionDS.OpenLoginEnlistTimeoutTimer 超时,SqlConnectionString连接选项,SqlCredential 凭证、字符串newPassword、SecureString newSecurePassword、, 布尔重定向EduseInstance at System.Data.SqlClient.SqlInternalConnectionDS..ctorDbConnectionPoolIdentity 标识、SqlConnectionString连接选项、SqlCredential 凭证、对象providerInfo、字符串newPassword、SecureString newSecurePassword、布尔重定向edUserInstance、SqlConnectionString 用户连接选项位于 System.Data.SqlClient.SqlConnectionFactory.CreateConnectionDbConnectionOptions 选项、DbConnectionPoolKey poolKey、对象poolGroupProviderInfo、, DbConnectionPool池,DbConnection owningConnection, DbConnectionOptions用户选项位于 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnectionDbConnectionPool 池,DbConnectionOptions选项,DbConnectionPoolKey池键, DbConnectionOptions用户选项位于 System.Data.ProviderBase.DbConnectionPool.CreateObjectDbConnectionOptions 用户选项位于 System.Data.ProviderBase.DbConnectionPool.UserCreateRequestDbConnectionOptions 用户选项位于 System.Data.ProviderBase.DbConnectionPool.TryGetConnectionDbConnection owningObject,UInt32 WaitForMultipleObjectStimOut,布尔型 allowCreate,仅布尔值EchkConnection,DbConnectionOptions userOptions、DbConnectionInternal和connection at System.Data.ProviderBase.DbConnectionPool.TryGetConnectionDbConnection owningObject、TaskCompletionSource1重试、DbConnectionOptions userOptions、DbConnectionInternal和connection at System.Data.ProviderBase.DbConnectionFactory.TryGetConnectionDbConnection owningConnection、TaskCompletionSource1重试、DbConnectionOptions userOptions、DbConnectionInternal和connection at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnectionDbConnection 外部连接,数据库连接工厂连接工厂, TaskCompletionSource1重试,DbConnectionOptions用户选项位于 System.Data.SqlClient.SqlConnection.TryOpenTaskCompletionSource`1 在System.Data.SqlClient.SqlConnection.Open处重试 WebApplication3.\u Default.Button1\u单击对象发送者,事件参数 电子客户端连接ID:0516c418-145d-4558-b9d7-ed2e55c74724

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


任何帮助都将不胜感激。

请与您的DBA联系,获取有关SQL Server实例上CHARMAN\JC-SRV$所需的权限

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

多亏了我添加的


到连接字符串,它工作正常

错误非常严重。.中的问题计算机不允许登录数据库。当我使用Trusted_连接时,我假设windows身份验证来自我的应用程序正在运行的上下文。在我的例子中,它通常是我站点的应用程序池的运行用户。在这些情况下,我从未在连接stri中使用过用户名和密码
ng。看起来您可能需要sql身份验证?@Andre我可以使用来自SMS和同一台机器的凭据访问数据库。。。当应用程序位于web服务器中时,这会有什么不同?@JasonEades我删除了trusted_连接,但仍然得到相同的错误。ID和密码用于SQL身份验证。您有什么建议吗?请尝试这样的连接字符串:Data Source=server;初始目录=MyDatabase;用户Id=blah;密码=废话;谢谢你的建议,但我只是在家里玩我自己的网络。因此没有人可以联系。如果您具有SA角色,则在需要从安全性连接的数据库上向该用户提供公共角色。至少是公共角色。
Integrated Security=False