简体   繁体   English

从存储过程获取结果以填充GridView

[英]Getting results from a stored procedure to populate a GridView

I have a windows aspx form that I have a TextBox , Button and a GridView . 我有一个Windows aspx表单,我有一个TextBoxButton和一个GridView The TextBox is stored as a variable @subschedule and passed to a stored procedure. TextBox存储为变量@subschedule并传递给存储过程。 What I'd like to do is to populate the results of that procedure into my GridView . 我想做的是将该过程的结果填充到我的GridView Can anyone suggest a way to do this? 有谁能建议这样做的方法?

Thank you 谢谢

Add a reference to System.Data.SqlClient 添加对System.Data.SqlClient的引用

Then create a method for your calling your stored procedure... Maybe wrap it up in a class for database calls. 然后创建一个方法来调用您的存储过程...也许将它包装在类中以进行数据库调用。

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);
            }
        }
    }
}

Then call it... 然后叫它......

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

Two popular options: 两个流行的选择:

1.. Code Behind: 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.. Use a SqlDataSource. 2 ..使用SqlDataSource。 This is a quick and dirty way to bind your ASP.NET server control to a stored procedure. 这是将ASP.NET服务器控件绑定到存储过程的快速而又脏的方法。 It's got its easy implementation pros, and some other cons : 它有简单的实现专业人员,以及其他一些缺点:

 <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>
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "SPPUBLISHER";
    adapter = new SqlDataAdapter(command);
    adapter.Fill(ds);
    connection.Close();
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();

Full Source.. gridview from procedure 完整来源.. 来自程序的gridview

You'll need to use the DataSource property: 您需要使用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();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM