【问题标题】:Getting output from return type DataSet and displaying it in a Gridview从返回类型 DataSet 获取输出并将其显示在 Gridview 中
【发布时间】:2026-01-27 11:50:02
【问题描述】:

我试图从返回类型为 DataSet 的方法中获取输出,并在 GridView 中使用它,但输出没有反映。 任何人都可以请建议如何获得输出。

  public DataSet GetData()
{
    try
    {
        SqlConnection conn = new SqlConnection(connectionString);
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        String sql = "Select top 100 * from SEQUENCE";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet output = new DataSet();

        adapter.Fill(output);
        conn.Close();

        return (output);     

    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, GetType(),
                   "ServerControlScript", ex.Message, true);

        return (null);

    }
}




    Home home = new Home();
    Output=home.GetData();
    GridViewOutput.DataSource = Output.Tables["Out"];
    GridViewOutput.DataBind();

【问题讨论】:

    标签: c# sql asp.net dataset


    【解决方案1】:

    尝试移动您声明output 并将其返回的位置,如下所示。

    我更改了您声明网格视图数据源的部分。您应该能够将数据集数据源声明为方法本身。

    看看这个关于数据集与数据表的线程Datatable vs Dataset 一个 DataSet 可以包含多个表。但是,如果您只返回单个结果集,则 DataTable 而不是 DataSet 可能更有意义。

    只需将方法类型更改为 DataTable。声明它的来源,如下所示。

    public DataSet GetData()
    {
        //Move where you declare output ot here
        DataSet output = new DataSet();
        try
        {
            SqlConnection conn = new SqlConnection(connectionString);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
    
            String sql = "Select top 100 * from SEQUENCE";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    
    
            adapter.Fill(output);
            conn.Close();
    
    
    
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(),
                       "ServerControlScript", ex.Message, true);
    
            return (null);
    
        }
         //And move the return to here 
         return output;
    }
    
    
    
        //Should just need this to display the data
        GridViewOutput.DataSource = GetData();
        GridViewOutput.DataBind();
    

    在 c# 中使用 SQL 时的最后一件事我倾向于使用 using 语句。它使代码更清晰,并为您处理资源的处置When should I use the using Statement?。如果您选择使用它,您的代码将如下所示:

     public DataTable GetData()
       {
          //Move where you declare output ot here
          var output = new DataTable();
    
          using (var conn = new SqlConnection())
          {
    
    
             try
                {
                    conn.ConnectionString = //Your DataBase Connection;
    
                    String sql = "Select top 100 * from SEQUENCE";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.commandType = commandType.Text;
    
    
                    Var adapter = new SqlDataAdapter(cmd);
    
    
                    adapter.Fill(output);
    
    
    
                }
                catch (Exception ex)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(),
                               "ServerControlScript", ex.Message, true);
    
                    return (null);
    
                }
                 //And move the return to here 
                 return output;
           }
      }          
    
    
       //Should just need this to display the data
       GridViewOutput.DataSource = GetData();
       GridViewOutput.DataBind();
    

    【讨论】: