.NET ASP C# - Datetime.now在插入时返回2天 [英] .NET ASP C# - Datetime.now return back 2 days when insert

查看:69
本文介绍了.NET ASP C# - Datetime.now在插入时返回2天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的朋友。

我的项目有一个很大的错误。当我插入新数据时,我的Datetime.now返回错误的值。例如,插入时:05/20/2015。它将在2天后返回:05/18/2015。

这是我的代码。



数据:

Hello my friend.
My project have a big mistake. when i insert a new data, my Datetime.now is returned bad values. Example, when insert: 05/20/2015. It will be back 2 day: 05/18/2015.
This is my Code.

Data:

   public bool add(Bill_DTO bill_dto) 
{ 
    try 
    {
        SqlCommand _cmd = new SqlCommand('INSERT INTO BILL('ID_BILL,TIMECOMING') VALUES(@ID_BILL,@TIMECOMING)')
        _cmd.Parameters.Add("ID_BILL", SqlDbType.NVarChar).Value = bill_dto.ID_Bill;
        _cmd.Parameters.Add("TIMECOMING", SqlDbType.Datetime).Value = bill_dto.TimeComing; 
        dp = new DataProvider(); 

        return dp.ExecuteNonQuery(_cmd) > 0;
    } 
    catch (Exception ex) 
    { 
        throw ex; 
    } 
    finally 
    { 
        dp.CloseConn(); 
    } 





商家:





Business:

Bill_DTO bill_dto = new Bill_DTO();
Bill_DATA bill_data = new Bill_DATA();
bill_dto.TimeComing= DateTime.now;
bill_data.add(bill_dto);





感谢您的帮助!!!



Thanks for your help !!!

推荐答案

DateTime.Now获取/客户端计算机上的日期和时间。您确定您使用的计算机是否处于当前日期和时间?
DateTime.Now gets the date and time on your/client's computer. Are you sure that the computer you are using is in current date and time?


更改您的sql语句以获取sql server日期并插入如下

change your sql statement to get sql server date and insert as below
INSERT INTO BILL('ID_BILL,TIMECOMING') VALUES(@ID_BILL,GETDATE())



请注意,现在只需将ID_BILL设置为参数。


note that now only ID_BILL need to set as parameter.


这篇关于.NET ASP C# - Datetime.now在插入时返回2天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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