I wish to display the output of the SQL Server command "sp_who2 active" in a WPF datagrid. I've come up with the following code -
private void GetActiveSQLIds()
{
SqlConnection con = new SqlConnection(S开发者_C百科TR_DataSource);
con.Open();
SqlCommand cmd = new SqlCommand("EXEC sp_who2 active", con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
this.dataGrid1.AutoGenerateColumns = true;
this.dataGrid1.ItemsSource = dt.Select();
con.Close();
}
It executes ok, but actually displays the columns "RowError", "RowState" etc, rather than the output of sp_who2.
Anyone know how to do what I want to accomplish?
Found it - just needed to change the second last line to -
this.dataGrid1.ItemsSource = dt.DefaultView;
this.dataGrid1.ItemsSource = (dt as IEnumerable);
精彩评论