从返回类型DataSet获取输出并将其显示在Gridview中

时间:2018-09-03 16:15:20

标签: c# sql asp.net dataset

我试图从返回类型为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();

1 个答案:

答案 0 :(得分:0)

尝试将声明为output并返回的位置移动到下面所示的位置。

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

在数据集和数据表Datatable vs 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();