错误sql:将varchar数据类型转换为datetime数据类型导致超出范围值 [英] Error sql:The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

查看:177
本文介绍了错误sql:将varchar数据类型转换为datetime数据类型导致超出范围值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码输入当天的日期:

I tried to enter the date of the current day with the following code:

string now = (DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year).ToString();
string tm = (DateTime.Today.Hour + ":" + DateTime.Today.Minute).ToString();
string sql2 = string.Format("INSERT INTO Kabala2 (Nu_kabala,Ma_num,Sk,Seif_hacnasa,Seif_name,Date) VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", n, Session["Ma_num"], lprice, lkod,des, now );
Dal.DoQuery(sql2);

当我运行此代码时,会显示标题中的错误:

when I run this code it shows me the error in the title :


将varchar数据类型转换为datetime数据类型导致
超出范围值

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

我该如何解决?

------------------编辑

------------------edit

我将其更改为: string now = DateTime.Today.ToString(yyyy-MM-dd HH:mm:ss); ,它的工作原理

i changed it to:string now = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss"); and it works.

现在我有另一个问题,当我运行下面的代码显示错误:将数据类型varchar转换为数字错误。

now i have another problem, when i run the code below it shows me the error:Error converting data type varchar to numeric.

string now = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");
    string tm = DateTime.Now.ToString("HH:mm:ss");
    string sql = string.Format("INSERT INTO Kabala1 (Nu_kabala,Ma_num,Date,Time,Total,Status,Name,User_n) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", n, Session["Ma_num"], now, tm, lprice, "ddffs", Session["user"], "ddffs");
    Dal.DoQuery(sql);
    string sql2 = string.Format("INSERT INTO Kabala2 (Nu_kabala,Ma_num,Sk,Seif_hacnasa,Seif_name,Date) VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", n, Session["Ma_num"], lprice, lkod, des, now);
    Dal.DoQuery(sql2);
    string sql3 = string.Format("INSERT INTO Kabala3 ((Nu_kabala,Msd,Ma_num,Kind_pay,Name_pay,Date_pay,Sk,Ms_sek,Snif,Bank,Date_klita,Seif) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", n,"1", Session["Ma_num"],"13", "ddffds", now, lprice, this.card.Text, this.mm.Text, this.yy.Text, now, lkod);
    Dal.DoQuery(sql3);


推荐答案

您可以使用 DateTime.Today.ToString(yyyy-MM-dd HH:mm:ss) //无论什么格式...

You can Use DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss") //Whatever format...

或者使用CONVERT方法直接使用sql语法是

or Use direct sql instead using CONVERT method. Syntax is


在您的情况下,使用103格式的日期

in your case, use the 103 format for the date



CONVERT(VARCHAR,DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year,103)




和108当时

and 108 for the time



CONVERT(VARCHAR, DateTime.Today.Hour + ":" + DateTime.Today.Minute + ":00" ,108)

必须提供格式hh:mm:ss

whould give format of hh:mm:ss

以下w3链接的完整参考:
http://www.w3schools.com/sql/func_convert.asp

full reference from the following w3 link: http://www.w3schools.com/sql/func_convert.asp

这篇关于错误sql:将varchar数据类型转换为datetime数据类型导致超出范围值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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