我想将日期和时间分别存储在数据库中 [英] I want store date and time separate in database

查看:75
本文介绍了我想将日期和时间分别存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有4列,当我登录时它会存储用户名以及日期和时间.但是我要和时间约会.我不想这样,我想存储单独的日期和时间

i have 4 columns in database, when i login it shold store username and date and time. but i am getting date with time. i dont want like that i want to store separate date and time

推荐答案

通常将它们分开存储不是一个好主意-DateTime变量不是日期和时间(例如"24/11/2008 13:45:16")是自商定的起点以来的毫秒数",因此专门存储时间会有些困难,并且会有些浪费. br/> 也许您想要做的是存储时间差(Timespan)呢?这可能是自午夜以来的毫秒数,然后可以将其应用于任何基本DateTime.
It''s not normally a good idea to store them separately - a DateTime variable is not a date and a time as in "24/11/2008 13:45:16" it is a "this number of milliseconds since an agreed starting point" so specifically storing a time alone is a bit difficult, and would be somewhat wasteful.
Perhaps what you want to do is store a difference in times (a Timespan) instead? That could be the number of milliseconds since midnight, which could then be applied to any base DateTime.


string timeonly = DateTime.Now.ToShortTimeString();
string dateonly = DateTime.Now.ToShortDateString();


CREATE TABLE UserLogin (
	ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	UserName NVARCHAR(20),
	LoginDate Date,
	LoginTime Time
)



如果您使用存储过程插入数据,则将其用作您的过程



IF YOU INSERT DATA WITH THE USE OF STOREPROCEDURE THEN USE THIS AS YOUR PROCEDURE

DECLARE @userName NVARCHAR(20);
--SET @userName = 'Tejas Vaishnav';
INSERT INTO UserLogin (UserName,LoginDATE,LoginTIME) VALUES (
@userName,
cast(convert(varchar,getdate(),101) as date),
cast(convert(varchar,getdate(),108) as time)
)



或者您希望从页面或c#
后面的代码中获取它 然后用这个



or you want it from your code behind page or c#
then use this

DateTime.Now.ToShortDateString();
DateTime.Now.ToShortTimeString();



有关DateTime和Convert或Cast的更多详细信息,请参见此链接
日期时间 [



For More Detail on DateTime and Convert or Cast see this link
DateTime[^]


这篇关于我想将日期和时间分别存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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