方法“ToString"没有重载;投射日期时需要 1 个参数 [英] No overload for method 'ToString" takes 1 arguments when casting date

查看:29
本文介绍了方法“ToString"没有重载;投射日期时需要 1 个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期从我的 Angular ui-Datepicker 保存到我的 SQL 数据库.日期采用 (10-27-2015 12:00 AM) 格式,但不会保存.我尝试使用以下方法将其转换为 SQL DateTime 格式:

I am trying to save a date from my Angular ui-Datepicker to my SQL database. The date is in the format (10-27-2015 12:00 AM) but it will not save. I tried using the following to convert it to SQL DateTime format:

    DateTime? myDate = form.dteStartDate;
    string sqlFormattedDate = myDate.ToString("yyyy-MM-dd HH:mm:ss");

但我收到错误消息方法 'ToString' 没有重载需要 1 个参数.SQL 中的字段类型为 'datetime'.

But I receive the error "No overload for method 'ToString' takes 1 arguments. The field in SQL is type 'datetime'.

非常感谢任何帮助.

推荐答案

您想使用 DateTime.ToString(format) 不是 Nullable.ToString(无重载):

DateTime? myDate = form.dteStartDate;
string sqlFormattedDate = myDate.Value.ToString("yyyy-MM-dd HH:mm:ss");

当然这不处理没有价值的情况.也许是这样的:

Of course this doesn't handle the case that there is no value. Perhaps something like this:

string sqlFormattedDate = myDate.HasValue 
    ? myDate.Value.ToString("yyyy-MM-dd HH:mm:ss")
    : "<not available>";

这篇关于方法“ToString"没有重载;投射日期时需要 1 个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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