仅使用日期搜索记录 [英] Search record with only with Date

查看:80
本文介绍了仅使用日期搜索记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用下拉列表和文本框搜索记录

数据类型是DateTime

在日期列中保存为28/11/2012 12:45:08完整的日期和时间

i使用此代码搜索记录其工作服务器但不记录客户端系统..





i want to search record using dropdown and text box
the data type is DateTime
in date column its saving as 28/11/2012 12:45:08 full date and time
i am using this code for searching record its working server but not on client systems..


 protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("SELECT * FROM Transactions WHERE report_type = @report_type AND " + "[R_date] >= @date_t_s and [R_date] < @date_t_e");

        cmd.Connection = con;
        cmd.Parameters.Add("@report_type", SqlDbType.NVarChar).Value = DropDownList1.SelectedValue;
        cmd.Parameters.Add("@date_t_s", SqlDbType.DateTime).Value = DateTime.Parse(TextBox1.Text);
        cmd.Parameters.Add("@date_t_e", SqlDbType.DateTime).Value = DateTime.Parse(TextBox1.Text).AddDays(1);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
}

推荐答案

问题可能与客户使用的文化有关 - 如果他们没有进入您的服务器文化设置所期望的格式的日期,那么传递给您的查询的日期将是错误的,或者当您尝试解析它时会导致异常。



您需要访问客户端日期设置并根据它进行解析,或使用一个控件返回DataTime值而不是字符串。
Probably, the problem is to do with the culture being used by the client - if they do not enter the date in the format expected by your servers Culture setting, then the date passed to your query will be wrong, or cause a exception when you try to parse it.

You need to either access the clients date setting and parse according to that, or use a control that returns you a DataTime value instead of a string.


select convert(varchar(10),Column_name ,103)作为Table_name的ShortDate

然后你只从你的专栏获得日期部分,如果你向你的客户提供ajax日历工具,那么他们可以轻松选择日期,并且符合你的要求然后你可以在查询之间轻松使用。
select convert(varchar(10), Column_name, 103) as ShortDate from Table_name
then you get only date part from your column and if you provide your client a ajax calender tool then they can easily pick the date and that is matched with your requirement then you can easily use between query.


您好,



您的客户端系统时间格式可能会因您的服务器系统时间格式而异。因此,在将文本框值解析为dateTime之后,您应该应用您的SQL日期时间格式



EX:

Hi,

your client system time format may be varied with your server system time format. So, after parsing the textbox value to dateTime you should apply your SQL date time format

EX:
DateTime.Parse(TextBox1.Text).ToString("yyyy-MM-dd hh:mm:ss");





--SJ



--SJ


这篇关于仅使用日期搜索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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