Archive for January, 2010

January 12, 2010

Getting execution plan through C#


private void Form1_Load(object sender, EventArgs e)

{

string strConn = “server=Localhost;database=AMT_DB;UID=sa;PWD=”;

SqlConnection conn = new SqlConnection(strConn);

SqlDataReader reader;

conn.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = conn;

cmd.CommandText = “SET SHOWPLAN_ALL ON”;

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

cmd.CommandText = “select * from script S inner join scriptline SL on S.scriptid=SL.scriptid”;

cmd.CommandType = CommandType.Text;

reader = cmd.ExecuteReader();

while (reader.Read())

{

MessageBox.Show(“physicalop ” + reader[4].ToString());

MessageBox.Show(“logicalop ” + reader[5].ToString());

MessageBox.Show(“argument ” + reader[6].ToString());

MessageBox.Show(“estimateio ” + reader[9].ToString());

MessageBox.Show(“estimatecpu ” +reader[10].ToString());

}

cmd.CommandText = “SET SHOWPLAN_ALL OFF”;

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

conn.close

}