【问题标题】:Getting results from a stored procedure to populate a GridView从存储过程中获取结果以填充 GridView
【发布时间】:2011-03-04 21:34:20
【问题描述】:

我有一个 Windows aspx 表单,我有一个 TextBoxButton 和一个 GridViewTextBox 存储为变量@subschedule 并传递给存储过程。我想做的是将该过程的结果填充到我的GridView 中。谁能建议一种方法来做到这一点?

谢谢

【问题讨论】:

    标签: asp.net data-binding gridview


    【解决方案1】:

    两个流行的选项:

    1.. 代码隐藏:

    string subSchedule = txtSubSchedule.Text.Trim();
    
    //you'll create a new class with a method to get a list of customers
    //from your database as others answers have demonstrated
    IEnumerable<Customer> custs = MyDataLayer.GetCustomers(subSchedule);
    
    myGrid.DataSource = custs;
    myGrid.DataBind();
    

    2.. 使用 SqlDataSource。这是一种将 ASP.NET 服务器控件绑定到存储过程的快速而肮脏的方法。它具有易于实施的优点,以及其他一些缺点:

     <asp:GridView  id="myGrid"
                runat="server"
                AutoGenerateColumns="true"
                DataSourceID="ds1" />
    
            <asp:SqlDataSource
                id="ds1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommandType="StoredProcedure"                
                SelectCommand="GetSchedule">
                  <SelectParameters>
                      <asp:ControlParameter name="SubSchedule" 
                              ControlID="txtSubSchedule" Propertyname="Text"/>
                  </SelectParameters>
            </asp:SqlDataSource>
    

    【讨论】:

      【解决方案2】:

      添加对System.Data.SqlClient的引用

      然后为调用存储过程创建一个方法...也许将其包装在一个类中以进行数据库调用。

      public static class DataBase
      {
          public static DataTable myProcedureName(String subSchedule)
          {
              var dt = new DataTable();
      
              using (var cnx = new SqlConnection("myConnectionString"))
              using (var cmd = new SqlCommand {
                  Connection = cnx,
                  CommandText = "myProcedureName", 
                  CommandType = CommandType.StoredProcedure,
                  Parameters = {
                      new SqlParameter("@subSchedule", subSchedule)
                  }
              })
              {
                  try
                  {
                      cnx.Open();
                      dt.Load(cmd.ExecuteReader());
                      return dt;
                  }
                  catch (Exception ex)
                  {
                      throw new Exception("Error executing MyProcedureName.", ex);
                  }
              }
          }
      }
      

      那就叫它吧……

      gvMyGrid.DataSource = DataBase.myProcedureName(txtSubSchedule.Text);
      gvMyGrid.DataBind();
      

      【讨论】:

        【解决方案3】:

        您需要使用DataSource 属性:

        DataTable dt = new DataTable();
        
        // Open the connection 
        using (SqlConnection cnn = new SqlConnection( 
               "Data Source=.\sqlexpress;Initial Catalog=AcmeRentals;Integrated Security=True")) 
        { 
            cnn.Open();
        
            // Define the command 
            using (SqlCommand cmd = new SqlCommand()) 
            { 
                cmd.Connection = cnn; 
                cmd.CommandType = CommandType.StoredProcedure; 
                cmd.CommandText = storedProcedureName;
        
                // Define the data adapter and fill the dataset 
                using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
                { 
                    da.Fill(dt); 
                } 
            } 
        } 
        
        // This is the key code; you can set the DataSource to "collection"
        gridView.DataSource = dt.DefaultView;
        gridView.DataBind();
        

        来源:http://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-retrieve-a-datatable-using-a-stored-procedure.aspx

        【讨论】:

        • Nate,我按照您的建议修改了代码,看到 Rows = {System.Data.DataRowCollection} 为 1,但在 GridView 中看不到任何数据。如果您需要查看代码当前的结构,我可以发布代码。
        • 您可能需要使用数据表的.DefaultView属性。
        • Nate,.defaultview 会从哪里进入我的代码?它会是关键代码的一部分吗?还是填充数据集的一部分?
        • 检查我的编辑,基本上是gridView.DataSource = dt.DefaultView;
        【解决方案4】:
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SPPUBLISHER";
            adapter = new SqlDataAdapter(command);
            adapter.Fill(ds);
            connection.Close();
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        

        完整来源..gridview from procedure

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-31
          • 1970-01-01
          • 2012-06-22
          • 1970-01-01
          相关资源
          最近更新 更多