1

I have an INSERT query like this

INSERT INTO tna_temp_attendance VALUES('" + strEmpCode + "', CONVERT(SMALLDATETIME, '" + dtDateTime.ToString() + "'), 0, " + _inOutMode.ToString() + ", null, 0, 'CHO-', '" + strMachine + "')

When I execute this query there is this error occurred "the conversion of a varchar data type to a datetime data type resulted in an out-of-range value"

Help Plz

My database has this table "tna_temp_attendance" and its columns are

  • Emp_id char(6)
  • Wdate smalldatetime
  • Is_read bit
  • Machine_Status bit
  • Process_Status bit
  • Is_Error bit
  • Loc_no char(4)
  • Ip varchar(16)

Edit

I passed the date like this also

CONVERT(SMALLDATETIME, '" + Convert.ToDateTime(dtDateTime.ToString("yyyy-MM-dd hh:mm:ss")) + "')

But it also not working

4

2 回答 2

3

我终于做到了。我声明了一个字符串。

  • 字符串 dt = DateTime.Now.ToString();

然后将其转换为 DateTime

DateTime ndt = Convert.ToDateTime(dt);

然后我用

CONVERT(SMALLDATETIME, CONVERT(DATETIME, '" + ndt + "'))

它奏效了。多谢你们

于 2016-02-18T03:38:03.367 回答
-1

尝试使用 ISO 格式将日期插入 SQL 服务器,如下所示:

dtDateTime.ToString("yyyyMMdd")
于 2016-02-17T03:24:25.003 回答