0

我有一个页面 status.aspx,其中包含几个按钮,例如 30 天、60 天和 90 天。我想根据单击的按钮将参数传递给我现有的gridview。我的gridview代码如下:

    Dim cmd As SqlCommand = New SqlCommand("up_AcctStatus", conn)
    cmd.CommandType = CommandType.StoredProcedure

    Dim paramNumOfDays As New SqlParameter("@NumOfDays", SqlDbType.Int)
    paramNumOfDays.Value = "??? 30, 60 or 90 ???"
    cmd.Parameters.Add(paramNumOfDays)

GridView1.DataSource = cmd.ExecuteReader() GridView1.DataBind()

4

1 回答 1

0

如果您不想使用除 sqlComand 之外的任何 ORM,您可以:

            protected void Button1_Click(object sender, EventArgs e)
            {
                Fill(60);
            }
            protected void Button2_Click(object sender, EventArgs e)
            {
                Fill(30);
            }

            private void Fill(int numberOfDays)
            {
                using (SqlConnection connection = new SqlConnection("connectionString"))
                {
                    connection.Open();

                    SqlDataAdapter sda = new SqlDataAdapter();
                    sda.SelectCommand = new SqlCommand("stored procedure name", connection);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    sda.SelectCommand.Parameters.AddWithValue("@NumOfDays", numberOfDays);

                    DataSet ds = new DataSet();
                    sda.Fill(ds, "result_table");

                    GridView1.DataSource = ds.Tables["result_table"];
                    GridView1.DataBind();
                }
            }

VB:

                Protected Sub Button1_Click(sender As Object, e As EventArgs)
                Fill(60)
                End Sub

                Protected Sub Button2_Click(sender As Object, e As EventArgs)
                    Fill(30)
                End Sub

                Private Sub Fill(numberOfDays As Integer)
                    Using connection As New SqlConnection("connectionString")
                        connection.Open()

                        Dim sda As New SqlDataAdapter()
                        sda.SelectCommand = New SqlCommand("stored procedure name", connection)
                        sda.SelectCommand.CommandType = CommandType.StoredProcedure
                        sda.SelectCommand.Parameters.AddWithValue("@NumOfDays", numberOfDays)

                        Dim ds As New DataSet()
                        sda.Fill(ds, "result_table")

                        GridView1.DataSource = ds.Tables("result_table")
                         GridView1.DataBind()
                    End Using
                End Sub
于 2013-01-22T18:04:26.180 回答