如何用visual studio C#做查询数据库

m261835646 2014-12-08 04:09:20
我现在和数据库联好了,显示的表的全部项
我想做一个查询功能,两个文本输入框,后面一个按钮,输入数值后,点按钮,显示两个数值之间的结果,
例如,文本框1输入2014/11/10,文本框输入2014/11/20,点击按钮以后,显示的表为两个时间段之间的数据,表的Y列为时间。
...全文
693 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-烟花雨季 2014-12-09
  • 打赏
  • 举报
回复
引用 5 楼 Syolin 的回复:
C# 代码里面拼接一下 SQL 就可以了:

StringBuilder strSql = new StringBuilder();
strSql.Append("select * from table where 1=1");
if(!string.IsNullOrEmpty(start_date) && !string.IsNullOrEmpty(end_date))
{
    strSql.AppendFormat(" and y between '{0} 00:00:00' and '{1} 23:59:59.999'",start_date,end_date);
}
else if(!string.IsNullOrEmpty(start_date))
{
    strSql.AppendFormat(" and y >='{0} 00:00:00'",start_date);
}
else{
    strSql.AppendFormat(" and y <= '{1} 23:59:59.999'",end_date);
}
start_date:你输入的开始日期 、end_date: 你输入的结束日期
+1
Lyndon_xl 2014-12-09
  • 打赏
  • 举报
回复
C# 代码里面拼接一下 SQL 就可以了:

StringBuilder strSql = new StringBuilder();
strSql.Append("select * from table where 1=1");
if(!string.IsNullOrEmpty(start_date) && !string.IsNullOrEmpty(end_date))
{
    strSql.AppendFormat(" and y between '{0} 00:00:00' and '{1} 23:59:59.999'",start_date,end_date);
}
else if(!string.IsNullOrEmpty(start_date))
{
    strSql.AppendFormat(" and y >='{0} 00:00:00'",start_date);
}
else{
    strSql.AppendFormat(" and y <= '{1} 23:59:59.999'",end_date);
}
start_date:你输入的开始日期 、end_date: 你输入的结束日期
江南小鱼 2014-12-09
  • 打赏
  • 举报
回复
根据你的要求,最终转换成数据库字符串,无外乎下面三条语句
select * from table where convert(varchar(8),Y,112) between '20141110' and '20141120'
select * from table where convert(varchar(8),Y,112) >= '20141110'
select * from table where convert(varchar(8),Y,112) <= '20141120'
_Monkey_King 2014-12-08
  • 打赏
  • 举报
回复
如果SQL语句有了 那不就是差个where条件而已啊
蝶恋花雨 2014-12-08
  • 打赏
  • 举报
回复
 
string sql="selecr *from where date";
  if (!txtDateB.Text.Trim().Length > 0)
        {
            sql += string.Format("date >= '{0}'" , txtDateB.Text.Trim());//选取录入日期大于查询日期的数据
        }
        if (!txtDateE.Text.Trim().Length > 0)
        {
            sql += string.Format("and date <= '{0}'", DateTime.Parse(txtDateE.Text.Trim()).ToString("yyyy-MM-dd") + " 23:59:59");//选取录入日期小于查询日期的数据
        } 
yzf86211861 2014-12-08
  • 打赏
  • 举报
回复
把这2个文本框的值 当做 2个参数 传到SQL 里面 用 between.... and ....... 应该可以了吧 注意处理下 2014/11/10 00:00:00 和 2014/11/20 23:59:59

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧