以dd / mm / yyyy格式的格式将datagridview日期保存在数据库中 [英] saving the datagridview date in the database in the format of dd/mm/yyyy format

查看:87
本文介绍了以dd / mm / yyyy格式的格式将datagridview日期保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意它是windows applciation。





我的设计如下;





当我选择日历时,所选月份日期显示在datagridview中。



Datagridview如下;



日期

11/3/2013。





MS Access中的数据库结构如下;



可用日期日期/时间





我的插入查询如下;



sql =插入Tb_Faculty_Availbility值Available_date+values(''+ Convert.ToDateTime(DGVCalendar) .Rows [i] .Cells [1] .Value.ToString())+'');





i想要保存数据库中的可用日期为dd / mm / yyyy格式。



我怎样才能在上面的sql查询中编写代码。



请帮帮我。



Rgds,

Narasiman P

note it is windows applciation.


My design as follows;


when i select the calendar, the selected month date is display into the datagridview.

Datagridview as follows;

Dates
11/3/2013.


Database Structure as follows in MS Access;

Available Dates Date/Time


My insert query as follows;

sql = " insert into Tb_Faculty_Availbility values Available_date " + " values ( ''" + Convert.ToDateTime(DGVCalendar.Rows[i].Cells[1].Value.ToString()) + "'');


i want to save the Available Date in the database as dd/mm/yyyy format.

for that how can i write the code in my above sql query.

Please help me.

Rgds,
Narasiman P

推荐答案

不要。不要以任何格式将数据保存在数据库中。将其保存在Date或DateTime列中 - 它们没有格式。日期格式只应在您输入并输出给用户时应用 - 在其他任何时候它们都会妨碍并使比较和计算变得困难。



所以保存它作为参数:

Don''t. Do not save the data in your database in any format. Save it in a Date or a DateTime column instead - they do not have a format. Date formats should only be applied when you input and output them to the user - at all other times they get in the way and make comparisons and calculations difficult.

So save it as a parameter:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO Tb_Faculty_Availbility (Available_date) VALUES (@AD)", con))
        {
        com.Parameters.AddWithValue("@AD", Convert.ToDateTime(DGVCalendar.Rows[i].Cells[1].Value.ToString()));
        com.ExecuteNonQuery();
        }
    }


这篇关于以dd / mm / yyyy格式的格式将datagridview日期保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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