为什么我得到这个错误的查询 [英] why i get this error for query

查看:47
本文介绍了为什么我得到这个错误的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           string str = "Select sponcorid,leftcount,rightcount,totcount,pair,createddate from Payout_Master Where createddate Between '" + Calendar1.SelectedDate.ToString("yyyy-MM-dd") + "' And '" + Calendar1.SelectedDate.ToString("yyyy-MM-dd") + "'";
               //string str = "SELECT sponcorid,leftcount,rightcount,totcount,pair,createddate FROM Payout_Master WHERE createddate='" + Calendar1.SelectedDate.ToShortDateString() + "'";
               com = new SqlCommand(str, con);
               com.CommandType = CommandType.Text;
               da = new SqlDataAdapter(com);
               DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    gvUserInfo.DataSource = dt;
                    gvUserInfo.DataBind();
                }

       }



}



错误:



将char数据类型转换为datetime数据类型会导致日期时间值超出范围。


}

error:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

推荐答案

如果没有您的错误消息,则很难准确,但问题可能是ToShortDateString对文化敏感,并且它返回的信息将取决于您运行的计算机的配置方式。由于SQL更喜欢yyyy-MM-dd格式的日期,请尝试使用ToString:

Without your error message it''s difficult to be precise, but it is likely that the problem is that ToShortDateString is culture sensitive, and the information it returns will depend on how the computer you are running on is configured. Since SQL prefers it''s date in yyyy-MM-dd format, try using ToString instead:
string str = "Select sponcorid,leftcount,rightcount,totcount,pair,createddate from Payout_Master Where createddate Between '" + Calendar1.SelectedDate.ToString("yyyy-MM-dd") + "' And '" + Calendar1.SelectedDate.ToString("yyyy-MM-dd") + "'";

但是,我怀疑你的查询是否会返回可能的记录,因为两个日期是相同的!你的意思是使用两个日历对象吗?

However, I doubt if your query will return may records, given that both dates are the same! Did you mean to use two Calender objects?


datetime dt = Calendar1.SelectedDate.ToShortDateString();



datetime dt=Calendar1.SelectedDate.ToShortDateString();

string str = "Select sponcorid,leftcount,rightcount,totcount,pair,createddate from Payout_Master Where createddate Between '" + dt + "' And '" + dt + "'";





在日期时间设置一个断点dt检查日期格式。



put a break point at datetime dt check the date format.


它表示你给的是日​​期但它不匹配使用数据库日期类型,因此您需要将日期转换为所需类型,然后放入查询可以解决您的问题..
it is stating that you are giving date but it is not match with database date type so you need to convert date to required type and then place in query may solve your problem..


这篇关于为什么我得到这个错误的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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