none
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
    EXEC
    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_con.Open();

    sql_reader = sql_command.ExecuteReader();

     

    while (sql_reader.Read())

    {

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

    }

     

    sql_reader.Close();

    sql_reader.Dispose();

    sql_command.Dispose();

    sql_con.Close();

    sql_con.Dispose();

     

    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

Answers