如何在数据库中存储datetime值?

问题描述:

private int  SaveToDB (string st ,string  brt,string  brot, string  spt)
{
    con1.Open();
    SqlCommand cmd = new SqlCommand("dattime", con1);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con1 ;
    cmd.Parameters.Add("@st", SqlDbType.DateTime ).Value = label2_time1.Text   ;
    cmd.Parameters.Add("@brt", SqlDbType.DateTime ).Value = label2_brkintime.Text  ;
    cmd.Parameters.Add("@brot", SqlDbType.DateTime ).Value = label3_brkofftm.Text ;
    cmd.Parameters.Add("@spt", SqlDbType.DateTime ).Value = label2_time2.Text ;
    cmd.ExecuteReader();

    con1.Open();
     int  tm = cmd.ExecuteNonQuery();
     con1.Close();
     return tm;

}

您没有发布SQL,但是请确保将SQL端的参数设置为日期时间数据类型.同样,现在,如果您这样做的话,请记住,您将"SaveToDB"方法中的参数作为字符串传递.您可能需要在传递它们之前将它们转换为DateTime对象,就像一个是字符串而另一个是日期时间一样,它将不接受它.但这还取决于他们如何在您的SPROC中进行设置.

如果仍然有问题,请发布您的存储过程.祝你有个美好的夜晚.希望您一切顺利.
You didn''t post your SQL, but make sure that you are setting the parameters on the SQL side as a datetime data type. Also, right now, if you are doing it that way, keep in mind that you are passing the parameters in the ''SaveToDB'' method as strings. You may want to convert them to a DateTime object before you pass them, as if one is a string and the other is a datetime, it won''t accept it; but it also depends on how they are setting it up in your SPROC.

If you are still having issues, please post your stored procedure. Have a great night. I hope all goes well for you.


您好,

这很简单.通常,由于该原因,strings不能直接转换为DateTime对象,日期格式可以是任何格式.因此,在将其分配给参数之前,请将字符串转换为日期时间对象.

希望对您有所帮助
Hi there,

This is simple. Often, strings cannot be directly casted to DateTime objects due to that, the date format can be anything. Therefore, before assigning it to the parameter, convert the string to a datetime object.

Hope this helps, regards