web页面查询数据库,GridView不显示数据
我在web页面添加了一个GridView,在页面load里面写了查询语句。但在运行后在页面上没有数据,也看不到GridView。数据库里是有相应数据的,是不是sql查询语句有错?
- C# code
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class 子页面_1 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { DateTime currentTime,a,b;//a和b为起止时间 currentTime = System.DateTime.Now; if (System.DateTime.Now.Hour >= 20) { a = currentTime.Date.AddHours(-3); b = a.AddHours(23); } else { a = currentTime.Date.AddHours(-27); b = currentTime.Date.AddHours(-4); } //查询时段a-b的数据 string ConStr = "server=IBM;user id=sa;pwd=sa;database=dataserver"; SqlConnection con = new SqlConnection(ConStr); string SqlStr = "select * from tb_hourdata where 时次>='" + a + "' and 时次<='" + b + "'order by 站号 ASC"; con.Open(); SqlDataAdapter da = new SqlDataAdapter(SqlStr, con); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); }}[解决办法]
你把
select * from tb_hourdata where 时次>='" + a + "' and 时次<='" + b + "'order by 站号 ASC
调试 出来,放到sql server查询下是否有数据
[解决办法]
设个断点看看就知道了