Getting xp_cmdshell results to display RRS feed

  • Question

  • I have a program that runs another program on another server by connecting to that server and running the xp_cmdshell procedure to execute it.  I wanted to know if anyone knows how to get the output from the program that is ran? 


    If I run the program in SQL Server with the line:

    Code Block
    xp_cmdshell 'program.exe arg1'


    I get the output from the program as a result as I would when running a normal select statement.  Now how would I get that same output to display in, let's say, a textbox?


    Here's my code that doesn't work...but you'll get my logic on how I think it should work given the above example ran in SQL Server...

    Code Block

    string query = "EXEC xp_cmdshell 'program.exe arg1'";

    string statusLog = "";


    SqlConnection sql_con = new SqlConnection(strCon);

    SqlCommand sql_command = new SqlCommand(query, sql_con);

    SqlDataReader sql_reader;



    sql_reader = sql_command.ExecuteReader();


    while (sql_reader.Read())


    statusLog += sql_reader.GetValue(0).ToString();









    This code, when replacing .ExecuteReader() with .ExecuteNonQuery(), runs the program on the server, but I don't get anything as the return value in statusLog.
    Friday, October 5, 2007 7:08 PM


  • It is related to permissions because when you run the code it runs under the context of your permission in the application you need to create a proxy to run the code, it is covered by Microsoft support in the link below.



    Wednesday, October 10, 2007 1:59 PM