I have a very peculiar problem. In the VS2010 Server Explorer, I can connect to a SQL Server and execute a stored procedure just fine. However, when I try to do this in the code I get an exception thrown:
The xp_cmdshell proxy account information cannot be retrieved or is
invalid. Verify that the ‘##xp_cmdshell_proxy_account##’ credential
exists and contains valid information.
Now maybe I used the wrong credentials in the code, but then I copied the connection string from the server explorer and put it into my connection string in my config file. Still, the same error.
Here is the code that does the connecting and calling of the stored procedure:
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;
}
And here is the relevant config XML for the connection string, which is copied from the connection string the Server Explorer uses:
<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>
Again, the Server Explorer can connect to the server and execute the stored procedure, but my code constantly throws the same Exception. What could be causing this?
Thank you
EDIT
To make it clear: the Server Explorer can both connect and execute stored procedures with the same connection string that my code uses. Yet my code throws an exception.
Does your sproc call xp_cmdshell?
If so, see:
http://msdn.microsoft.com/en-us/library/ms175046.aspx