No.6(使用DataView对数据排序)
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strCon = "Data Source=ahxh-02;Initial Catalog=PubBase;Integrated Security=True"; SqlConnection connection = new SqlConnection(strCon); connection.Open(); SqlCommand command = new SqlCommand("getinfo",connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable dt = new DataTable("table1"); adapter.Fill(dt); DataView dv = new DataView(); dv.Table = dt; dv.Sort = "productId desc"; dv.RowFilter = "productName ='黄山牌香烟'"; this.GridView1.DataSource = dv; this.GridView1.DataBind(); } }
create database PubBase use PubBase create table Product ( productId int primary key identity(1,1), productName varchar(20) ) insert into product values('黄山牌香烟') insert into product values('贵州牌香烟') insert into product values('云南牌香烟') insert into product values('金龙王牌香烟') insert into product values('小熊猫香烟') --存储过程1 create proc GetProInfo as select * from Product exec GetProInfo --存储过程2 create proc GetProInfo2 ( @id int ) as select * from Product where productId=@id exec GetProInfo2 2