在2个日期之间选择数据库中的行 [英] Select Rows in database between 2 dates

查看:57
本文介绍了在2个日期之间选择数据库中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Visual Studio 2005用于一个项目,我想在开始日期之间获取数据.以ASP.NET格式结束日期我正在使用SQL Server 2008作为后端& C#.

Hi I am using Visual Studio 2005 for a project and I want to get data between a start date & end date in an ASP.NET form I am using SQL Server 2008 as backend & C#.

con.Open();
SqlCommand cmd = new SqlCommand("select company_name,body_model from pur_body where date between '" + DateTime.Parse(TextBox1.Text).ToLongDateString() + "' and '" + DateTime.Parse(TextBox2.Text).ToLongDateString()+ "'", con);

      GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();



此代码显示的消息比字符串未被识别为有效的DateTime"

请帮帮我....



this code display message than "string was not recognized as a valid DateTime"

Please help me out ....

推荐答案

使用此代码,我认为它将解决您的问题

use this code i think it will solve your problem

con.Open();

	DateTime dt1, dt2;
        DateTime.TryParse(TextBox1.Text, out    dt1);
        DateTime.TryParse(TextBox2.Text, out    dt2);

SqlCommand cmd = new SqlCommand("select company_name,body_model from pur_body where date between '" + dt1 + "' and '" + dt2 + "'", con);
 
      GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();


好的,这里的相关问题似乎是:输出字符串的格式是什么?
从mySql数据库复制的示例日期是:"2012-06-11 00:00:00"

也许 [
Okay, the pertinant question here seems to be: What is the format of the output string?
An example date copied from my mySql database is: "2012-06-11 00:00:00"

Perhaps this[^] page will help.


con.Open();
 
	DateTime dt1, dt2;
        DateTime.TryParse(TextBox1.Text, out    dt1);
        DateTime.TryParse(TextBox2.Text, out    dt2);
 
SqlCommand cmd = new SqlCommand("select company_name,body_model from pur_body where date >= '" + dt1 + "' and 
<= '" + dt2 + "'", con);
 
      GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();


这篇关于在2个日期之间选择数据库中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆