无过载方法'的ToString"需要1个参数铸造日期时 [英] No overload for method 'ToString" takes 1 arguments when casting date

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

问题描述

我想从我的角度UI的日期选择器的日期保存到我的SQL数据库。日期的格式为(2015年10月27日12:00),但它不会保存。我试着用下面将其转换为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类型为日期时间。

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

任何帮助是极大的赞赏。

Any assistance is greatly appreciated.

推荐答案

您想使用 则DateTime.ToString(格式) 没有的 可空< D​​ateTime的>的ToString(无过载)

You want to use DateTime.ToString(format) not Nullable<DateTime>.ToString(no overload):

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&QUOT;需要1个参数铸造日期时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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