当前位置:编程学习 > C#/ASP.NET >>

asp.net 问题

sql语句是这样的
select * from infos where zcrq=@sj
sj 时间~
zcrq是注册日期smalldatetime
页面上有5个按钮 他们的text分别是2008 2009 2010 2011 2012
我要实现的功能是这样的
点击按钮 从infos表中查找出对应年份的内容 放到gridview中在页面中显示~
有什么办法么?
新手弄了好久也没有实现~

 

答案:
查出数据来是dataset,用GridView的datasource绑定这个dataset,然后databing一下就行

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string s = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();//连接的字符串,在web.config中写,在这里调用
                SqlConnection con = new SqlConnection(s);
                string s1 = "select * from infos where zcrq=@sj";
                /*或者
                 SqlCommand cmd = new SqlCommand(s1, con);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                DataGrid1.DataSource  = dr;*/
                SqlDataAdapter ada = new SqlDataAdapter(s1, con);
                DataSet ds = new DataSet();
                ada.Fill(ds);
                this.GriView1.DataSource = ds.Tables[0].DefaultView;
                GriView1.DataBind();//必须绑定
                con.Close();
            }
        }

需要控件才能显示的 可以是GridView FormView 或者DetailsView 都可以

上一个:asp.net控件
下一个:JAVA与ASP有什么区别?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,