我有一个非常特殊的问题。在 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>
同样,服务器资源管理器可以连接到服务器并执行存储过程,但我的代码不断抛出相同的异常。这可能是什么原因造成的?
谢谢
编辑
为了清楚起见:服务器资源管理器可以使用我的代码使用的相同连接字符串连接和执行存储过程。然而我的代码抛出了一个异常。