功能:輸入兩個值傳入至DB中調用SP,依此兩個值為上限與下限查找資料。
1.畫面
2.SP
create procedure GetInfoByTwoID
(
@ID1 int,
@ID2 int
)
as
select * from 員工 where 員工編號 >= @ID1 and 員工編號 <= @ID2
order by 員工編號 asc
return
3. 後端程式碼
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//1.宣告cmd且連接資料庫
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["資料庫連接字串"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;
//2.指定要執行之type為sp
cmd.CommandText = "GetInfoByTwoID";
cmd.CommandType = CommandType.StoredProcedure;
//3.綁定傳入參數
cmd.Parameters.AddWithValue("@ID1", Convert.ToInt32(TextBox1.Text));
cmd.Parameters.AddWithValue("@ID2", Convert.ToInt32(TextBox2.Text));
//4.dataadpter與dataset
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
//用dataadpter填入資料到dataset的datatable(table name為取得資訊)
da.Fill(ds, "取得資訊");
GridView1.DataSource = ds.Tables["取得資訊"];
GridView1.DataBind();
}
}
執行結果:
留言列表