asp.net访问sql出错
con.Open();//打开数据库连接if(y1>y2 )
Sql = "select * from TestData " + "where [Year] between '" + y1 + "' and '" + y2 + "'and [Month] between'" + m1 + "'and'" + m2 +12+ "'and [Date] between'" + d1 + "'and'" + d2+30 + "'";
if (y1 == y2 && m1 == m2)
Sql = "select * from TestData " + "where [Year]='"+y1 +"' and [Month]='"+m1 +"' and [Date] between'" + d1 + "'and'" + d2 + "'";
else if (y1 == y2&&d1 ==d2 )
Sql = "select * from TestData " + "where [Year]='" + y1 + "' and [Month] between'" + m1 + "'and'" + (m2-1) + "'and [Date] between'" + d1 + "'and'" + d2 + 30 + "'";
else if (y1==y2 )
Sql = "select * from TestData " + "where [Year]='" + y1 + "' and [Month] between'" + m1 + "'and'" + m2 + "'and [Date] between'" + d1 + "'and'" + d2 + "'";
else
Response.Write("<script language=javascript> alert('输入时间有误')</script>");
SqlDataAdapter sqladp = new SqlDataAdapter(Sql, con);//创建SqlDataAdapter对象实例
DataTable dt = new DataTable();//初始化DataTable对象
//{
int a = dt.Rows.Count;
sqladp.Fill(dt);//填充DataTable对象
GridView1.DataSource = dt;//设置Gridview的数据源
GridView1.DataBind();//数据绑定
上面是我的一段程序代码,当y1>y2时执行到sqladp.Fill(dt);报错ExecuteReader: CommandText 属性尚未初始化,而y1=y2时并没有错误,我想知道这是什么原因,我这个查询语句应该怎么写?
另外,我想知道数据库中符合查询条件的有几行,我应该怎么获得这个值?当我使用int a = dt.Rows.Count;时,a的值总是为0,为什么,我应该怎么做才能取到行数?
多谢啦! --------------------编程问答-------------------- 什么乱七八糟的
没见过几个程序猿这么写代码的
--------------------编程问答-------------------- 注意单引号,空格之类的设置.
year,month,day 函数返回的是数值型的,没必要加单引号.
必要时,可以 Response.Write sql,在页面上看看语句是否正确,还可以把语句拷到SQL的SSMS里执行看是否有错. --------------------编程问答-------------------- 直接复制过来的,格式就成这样的了啊。不就是几个if...else语句吗 --------------------编程问答-------------------- 推荐用命令传参,不要简单拼接字符串
实在要偷懒,也应该用datapart(year, getdate()) between和强类型参数 --------------------编程问答-------------------- 又是空格的问题,看下面部分红色部分,剩下的你自己改吧
con.Open();//打开数据库连接
if(y1>y2 )
Sql = "select * from TestData " + "where [Year] between '" + y1 + "' and '" + y2 + "'and [Month] between'" + m1 + "'and'" + m2 +12+ "'and [Date] between'" + d1 + "'and'" + d2+30 + "'";
if (y1 == y2 && m1 == m2)
Sql = "select * from TestData " + "where [Year]='"+y1 +"' and [Month]='"+m1 +"' and [Date] between'" + d1 + "'and'" + d2 + "'";
else if (y1 == y2&&d1 ==d2 )
Sql = "select * from TestData " + "where [Year]='" + y1 + "' and [Month] between'" + m1 + "'and'" + (m2-1) + "'and [Date] between'" + d1 + "'and'" + d2 + 30 + "'";
==============》
--------------------编程问答-------------------- LZ贴图吧!!!!! --------------------编程问答-------------------- GridView1.DataSource = dt.DefaultView;//设置Gridview的数据源
GridView1.DataBind();//数据绑定
--------------------编程问答-------------------- 估计就是sql语句问题 好好检查检查吧 --------------------编程问答-------------------- 放断点在每个sql中,复制出来到数据库查看下有没错~...建议用stringbuilder
补充:.NET技术 , C#