节省时间和日期到SQL数据库 [英] Save time and date to sql database

查看:72
本文介绍了节省时间和日期到SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将当前时间和日期保存到数据库的Web方法。我想保存学生成功注册时的当前时间和日期。我有标签形式的日期和时间

I have a web method that saves the current time and date to the database. I want to save the current time and date when a student has successfully registered. I have the date and time in a label form

private void timer1_Tick(object sender, EventArgs e)
        {
            lbTime.Text = DateTime.Now.ToLongTimeString();

        }




lbDate.Text = DateTime.Now.ToLongDateString();

< br $> b $ b



Attendance record = new Attendance();
     record.StudentID = a.StudentID;
     record.RegistrationDate = lbDate.Text;
     record.RegistrationTime = lbTime.Text;





如何将其保存到数据库中。数据库中数据的数据类型是日期和时间,如时间(7)



How do I save this onto the database. Datatype for Data in the DB is date and time as Time(7)

推荐答案

根本不需要处理代码中的日期和时间,请保留到sql server,

1.数据库表中不需要单独的日期和时间字段,只需要一个日期时间字段。

2.设置使用getdate()将此datetime字段的默认值设置为当前日期。有了它,每当你在表格中插入一行(新学生注册)时,当前的日期和时间就会自动插入到这个字段中。

阅读更多: getdate [ ^ ]
There is no need to handle the date and time in your code at all, leave it to the sql server,
1. There is no need to have separate date and time fields in the database table, just one datetime field will do.
2. Set the default value of this datetime field to the current date using getdate(). With that, whenever you insert a row (new student registration) into the table, the current date and time are automatically inserted into this field.
Read more: getdate[^]


保存在一个类型 DateTime 的数据库列中。



使用SQLParameter类创建对象以存储INSERT或UPDATE语句的参数。使用 DateTime.Now 将日期和时间设置为正确的参数。
Save in one database column of type DateTime.

Use SQLParameter Class to create objects to store the paramaters for your INSERT or UPDATE statement. Use DateTime.Now to set the date and time into the proper parameter.


这篇关于节省时间和日期到SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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