本文介绍了在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 ....

推荐答案

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();



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个日期之间选择数据库中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:29