如果您不想使用除 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