Gridview没有使用数据绑定填充

时间:2014-06-16 10:22:37

标签: c# asp.net

我有以下ASP gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="TEXT" HeaderText="TEXT" SortExpression="TEXT" />
                <asp:BoundField DataField="session_id" HeaderText="session_id" SortExpression="session_id" />
                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
                <asp:BoundField DataField="command" HeaderText="command" SortExpression="command" />
                <asp:BoundField DataField="cpu_time" HeaderText="cpu_time" SortExpression="cpu_time" />
                <asp:BoundField DataField="total_elapsed_time" HeaderText="total_elapsed_time" SortExpression="total_elapsed_time" />
                <asp:BoundField DataField="dbname" HeaderText="dbname" ReadOnly="True" SortExpression="dbname" />
                <asp:BoundField DataField="user_running_query" HeaderText="user_running_query" SortExpression="user_running_query" />
            </Columns>
        </asp:GridView>

在我背后的代码中:

protected void Page_Load(object sender, EventArgs e)
        {   
            using (Repository.SqldbAllQueries())
            {
                GridView1.DataBind();
            }    
        }

我的Repository类包含:

public static class Repository
    {
        public static SqlDataReader SqldbAllQueries()
        {
            SqlConnection sqlConnection1 = new SqlConnection("Data Source=sqldb;Initial Catalog=ReportServer;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            cmd.CommandText = @"SELECT sqltext.TEXT,req.session_id,req.status,req.command,req.cpu_time,
                                req.total_elapsed_time,DB_NAME(req.database_id) as dbname,s.login_name as user_running_query
                                FROM sys.dm_exec_requests req
                                CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext 
                                INNER JOIN sys.dm_exec_sessions s ON req.session_id = s.session_id";

            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;            
            sqlConnection1.Open();
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }        
    }

我试图捕获sql数据库中当前正在运行的每个查询,并在我的ASP页面中使用databind方法作为网格视图输出,代码构建并运行但是gridview不会在页面上显示。我已经逐步完成了代码,我可以看到reader包含行,但我似乎在这里遗漏了一些东西,因为它们没有被添加到gridview中,有人能看到问题吗?

2 个答案:

答案 0 :(得分:1)

尝试这个,

页面加载

中的

 GridView1.DataSource = Repository.SqldbAllQueries();
 GridView1.DataBind();

答案 1 :(得分:1)

试试这段代码。实际上,如果你退回读者,你可能会遇到例外。

protected void Page_Load(object sender, EventArgs e)
{
    DBAccess db = new DBAccess();
    db.FetchData(BindData);
}

private void BindData(SqlDataReader reader)
{
    DataGridView1.DataSource = reader;
    DataGridView1.DataBind();
}

这里是DBAccess类(类似于Repository类)

public void FetchData(Action<SqlDataReader> bindMethod)
{
    using (SqlConnection sqlConnection = new SqlConnection("ConnectionString"))
    {
        sqlConnection.Open();
        using (SqlCommand sqlCommand = new SqlCommand("Query", sqlConnection))
        {
            bindMethod.Invoke(sqlCommand.ExecuteReader(CommandBehavior.CloseConnection));
        }
    }
}

希望此代码有所帮助。