首页 > 解决方案 > 使用日期时间选择器将日期插入数据库

问题描述

这是我第一次在 C# 中使用日期时间选择器。
下面是我的插入数据用户控件。它将所有文本框中存在的所有数据添加到 DB.Neglect,但订单日期和调用日期都不起作用。

数据库中所有当前日期的数据类型为“日期”。
当我运行程序并插入并单击当前日期的 txtbox 时,它会自动填充今天的日期 [按程序],但它是以“dd/mm/yyyy”的格式插入的。

但早些时候,当我在文本框中手动输入日期时,格式将是“yyyy-mm-dd”。早些时候它成功地将日期插入数据库,但现在没有。

我想这是因为日期的格式,我在我的代码中遗漏了一些东西:

private bool insertdata()
        {
           //I've included the value of current date text box in string format which I think may be causing problem 
                string custname = txtcustname.Text, custcat = cmbcustcat.Text, date = txtdate.Text, phoneno = txtphoneno.Text, dresstype = cmbtype.Text, pattern = cmbpattern.Text, height = txtheight.Text, waist = txtwaist.Text, padar = txtpadar.Text, advance = txtadvance.Text;
                if (custname != null && custcat != null && date != null && phoneno != null && dresstype != null && pattern != null && height != null && waist != null && padar != null)
                {
                    cmd = new SqlCommand("INSERT INTO ordertbl(cust_name,cust_cat,date,phoneno,dress_type,pattern,height,waist,padar,adv) VALUES('" + custname + "','" + custcat + "','" + date + "','" + phoneno + "','" + dresstype + "','" + pattern + "','" + height + "','" + waist + "','" + padar + "','" + advance + "')", con);
                    cmd.ExecuteNonQuery();
                    return true;
                }
                else 
                {
                    return false;
                }
           //this method is called in the add data button


        }
        private void calenderdate(TextBox txtbox)
        {
            //autofills textbox with current date...The drop down arrow beside the txtbox is the DTP and it is initially not visible.
            if (calcurrentdate.Visible == false)
            {
                calcurrentdate.Visible = true;
                txtbox.Text = calcurrentdate.Value.ToShortDateString();
            }
        }
private void txtdate_Click(object sender, EventArgs e)
        {
            //when the text box of currentdate is clicked it is auto filled with today's date
            calenderdate(txtdate);
        } 

标签: c#datedatetimepicker

解决方案


推荐阅读