0

我有一个非常特殊的问题。在 VS2010 Server Explorer 中,我可以很好地连接到 SQL Server 并执行存储过程。但是,当我尝试在代码中执行此操作时,会引发异常:

xp_cmdshell 代理帐户信息无法检索或无效。验证“##xp_cmdshell_proxy_account##”凭据是否存在并包含有效信息。

现在也许我在代码中使用了错误的凭据,但后来我从服务器资源管理器中复制了连接字符串并将其放入我的配置文件中的连接字符串中。仍然,同样的错误。

下面是连接和调用存储过程的代码:

public static DataSet callStoredProcedure(string procedure, params SqlParameter[] args)
{
    SqlDataAdapter adapter = new SqlDataAdapter();
    DataSet dataSet = new DataSet();

    string connString = ConfigurationManager.ConnectionStrings["App"].ConnectionString;

    try
    {
        using (SqlConnection conn = new SqlConnection(connString))
        using (SqlCommand command = conn.CreateCommand())
        {
            conn.Open();
            if (args != null)
            {
                foreach (SqlParameter param in args) command.Parameters.Add(param);
            }
            command.CommandText = procedure;
            command.CommandType = CommandType.StoredProcedure;

            adapter.SelectCommand = command;
            adapter.Fill(dataSet, "tabela");
        }
    }
    catch (SqlException exception)
    {
        throw new Exception("SqlClient exception", exception);
    }

    return dataSet;
}

这是连接字符串的相关配置 XML,它是从服务器资源管理器使用的连接字符串中复制的:

  <connectionStrings>
    <add name="App" connectionString="Data Source=db2.myapp.com,49178\hosting;Initial Catalog=app;User ID=appuser;Password=f00barbaz" providerName="System.Data.SqlClient" />
  </connectionStrings>

同样,服务器资源管理器可以连接到服务器并执行存储过程,但我的代码不断抛出相同的异常。这可能是什么原因造成的?

谢谢

编辑
为了清楚起见:服务器资源管理器可以使用我的代码使用的相同连接字符串连接和执行存储过程。然而我的代码抛出了一个异常。

4

2 回答 2

1

你的存储过程调用 xp_cmdshell 吗?

如果是这样,请参阅:

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

当不是 sysadmin 固定服务器角色成员的用户调用它时,xp_cmdshell 使用存储在名为 xp_cmdshell_proxy_account 的凭据中的帐户名和密码连接到 Windows。如果此代理凭据不存在,xp_cmdshell 将失败。

可以通过执行 sp_xp_cmdshell_proxy_account 创建代理帐户凭据。作为参数,此存储过程采用 Windows 用户名和密码。例如,以下命令为具有 Windows 密码 sdfh%dkc93vcMt0 的 Windows 域用户 SHIPPING\KobeR 创建代理凭据。

于 2012-02-25T20:21:34.320 回答
0

您可以调试并确认来自 web.config 的连接字符串实际上正在加载到 connString 变量中吗?

另外,这Data Source=db2.myapp.com,49178\hosting看起来很奇怪。我看到服务器,端口,但什么是托管?

Initial Catalog=app is app the name of your DB?
于 2012-02-25T20:19:14.613 回答