如何在数据库中插入datetimepicker [英] how to insert datetimepicker in the database

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

问题描述

你好

plz解决这个如何添加datetimepicker







con.Open();

字符串q =插入到Development_Detal值(''+ Ename.Text +'',''+ UserPass.Text +'','' + EmpID.Text +'',''+ address.Text +'',''+ emaill.Text +'','' + dateTimePicker1.Value.ToShortDateString() +'',''+ mobNo.Text +'',''+ city.Text +'');



SqlCommand cmd = new SqlCommand(q,con);





if((Ename.Text ==)||(UserPass。文字==)||(EmpID.Text ==)||(address.Text ==)||(mobNo.Text ==)||(city.Text ==) )

{

MessageBox.Show(必须填写所有数据);

}

else

{

cmd.ExecuteNonQuery();

MessageBox.Show(员工注册成功);

}

hello
plz solve this how to add datetimepicker



con.Open();
String q = "insert into Development_Detal values(''" + Ename.Text + "'',''" + UserPass.Text + "'',''" + EmpID.Text + "'',''" + address.Text + "'',''" + emaill.Text + "'',''"+dateTimePicker1.Value.ToShortDateString()+"'', ''" + mobNo.Text + "'',''" + city.Text + "'' )";

SqlCommand cmd = new SqlCommand(q, con);


if ((Ename.Text == "") || (UserPass.Text == "") || (EmpID.Text == "") || (address.Text == "") || (mobNo.Text == "") || (city.Text == ""))
{
MessageBox.Show("Must be fill all data ");
}
else
{
cmd.ExecuteNonQuery();
MessageBox.Show("Employee Registration Successfully");
}

推荐答案

您需要使用sql默认格式(YYYY-MM-DD)格式化日期。



You will need to format your date with sql default format (YYYY-MM-DD).

/// <summary>
/// ToSqlDate
///
/// converts datetime object to datetime string
/// with format YYYY-MM-DD HH:MM:SS
/// </summary>
/// <param name="dt">datetime object</param>
/// <returns>string with YYYY-MM-DD HH:MM:SS format</returns>
public string ToSqlDate(DateTime dt)
{
    return string.Format
        (
            "{0}-{1}-{2} {3}:{4}:{5}",
            dt.Year.ToString("0000"),
            dt.Month.ToString("00"),
            dt.Day.ToString("00"),
            dt.Hour.ToString("00"),
            dt.Minute.ToString("00"),
            dt.Second.ToString("00")
        );
}





然后你这样称呼:





Then you call it like this:

String q = "insert into Development_Detal values(''" + Ename.Text + "'',''" + UserPass.Text + "'',''" + EmpID.Text + "'',''" + address.Text + "'',''" + emaill.Text + "'',''"+ToSqlDate(dateTimePicker1.Value)+"'', ''" + mobNo.Text + "'',''" + city.Text + "'' )";


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

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