如何在SQL数据库中只插入日期而不是日期时间? [英] How to insert only date instead of datetime in SQL database ?

查看:573
本文介绍了如何在SQL数据库中只插入日期而不是日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在没有datetimepicker1的情况下将某些数据插入数据库时​​添加日期。



一切似乎都很好,直到默认时间显示日期。



有没有摆脱那个时间显示在数据库中?



我是什么尝试过:



Private Sub Submit_Click(发件人作为对象,e作为EventArgs)处理Submit.Click

Dim todayDate As Date = Date.Now.ToString(yyyy-MM-dd)

Dim命令As New SqlCommand(INSERT into EmployeeDetails(AddedOn)Values(@AddedOn))

command.Parameters.Add(@ AddedOn,SqlDbType.VarChar).Value = getDate

End Sub

Well I'm trying to add only date when some data inserted into database without datetimepicker1.

Everything seems fine till the default time showing along with date.

Is there anyways to get rid of that time showing in database ?

What I have tried:

Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
Dim todayDate As Date = Date.Now.ToString(" yyyy-MM-dd ")
Dim command As New SqlCommand("INSERT into EmployeeDetails(AddedOn)Values(@AddedOn)")
command.Parameters.Add("@AddedOn", SqlDbType.VarChar).Value = getDate
End Sub

推荐答案

Dim command As New SqlCommand("INSERT into EmployeeDetails(AddedOn)Values(@AddedOn)")
command.Parameters.AddWithValue("@AddedOn", Date.Today)



注意:如果您的列类型定义为 datetime datetime2 ,这将存储时间 00:00:00 的日期。如果您想存储日期而不任何时间信息,请将列类型更改为日期



日期和时间类型| Microsoft Docs [ ^ ]


NB: If your column type is defined as datetime or datetime2, this will store the date with a time of 00:00:00. If you want to store the date without any time information, then change your column type to date.

Date and time types | Microsoft Docs[^]


这篇关于如何在SQL数据库中只插入日期而不是日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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