gpt4 book ai didi

c# - 错误 SQL :The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

转载 作者:行者123 更新时间:2023-11-29 06:37:21 26 4
gpt4 key购买 nike

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

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

当我运行这段代码时,它会在标题中显示错误:

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

我该如何解决?

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

我将其更改为:string now = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss"); 并且有效。

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

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")//任何格式...

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

in your case, use the 103 format for the date

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

and 108 for the time

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

应该给出 hh:mm:ss 的格式

来自以下 w3 链接的完整引用: http://www.w3schools.com/sql/func_convert.asp

关于c# - 错误 SQL :The conversion of a varchar data type to a datetime data type resulted in an out-of-range value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23722215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com