保存“日期”在DataBase中使用Forms [英] Saving of "Date" in DataBase using Forms

查看:115
本文介绍了保存“日期”在DataBase中使用Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个学校(信息系统)数据管理系统的项目。我要保存数据库中学生的录取日期。

我混淆了保存数据库中日期的最佳方法。

有很多选择。即,在SQL服务器的表列定义中已经给出了DateDataType。

另一个datetimepicker控件以C#形式提供。

建议我在数据库中保存日期的更好方法是在项目的进一步开发中易于处理。

I'm working on a project of school(information system) data management system.I've to save the Date of admission of students in Database.
i'm confuse what is the best and easy method to save the "Date" in database.
There are many options . i.e. there is a "Date"DataType already given in table column definition of SQL server.
another "datetimepicker" control is available in C# forms.
suggest me the better way to save the date in database that will be easy to handle in further development of project.

推荐答案

使用DateTimePicker控件,并从它的Value属性中收集用户输入 - 这将为您提供一个保证有效的DateTime值。



然后使用参数化查询来传递DateTime值直接指向SQL并将其存储在DATE列中。

Use the DateTimePicker control, and collect the user input from it's Value property - this hands you a DateTime value which is guaranteed to be valid.

Then use a parameterised query to pass the DateTime value directly to SQL and store it in a DATE Column.
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myDateColumn) VALUES (@DT)", con))
        {
        cmd.Parameters.AddWithValue("@DT", myDateTimePicker.Value);
        cmd.ExecuteNonQuery();
        }
    }


这篇关于保存“日期”在DataBase中使用Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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