Question

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.

Was it helpful?

Solution

Does your sproc call xp_cmdshell?

If so, see:

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

When it is called by a user that is not a member of the sysadmin fixed server role, xp_cmdshell connects to Windows by using the account name and password stored in the credential named xp_cmdshell_proxy_account. If this proxy credential does not exist, xp_cmdshell will fail.

The proxy account credential can be created by executing sp_xp_cmdshell_proxy_account. As arguments, this stored procedure takes a Windows user name and password. For example, the following command creates a proxy credential for Windows domain user SHIPPING\KobeR that has the Windows password sdfh%dkc93vcMt0.

OTHER TIPS

can you debug and confirm that the conenction string from the web.config is actually beeing loaded into the connString variable?

also, this Data Source=db2.myapp.com,49178\hosting looks strange. I see server,port but what is hosting?

Initial Catalog=app is app the name of your DB?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top