DataTimePicker和访问数据库 [英] DataTimePicker and Access Database

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

问题描述

你好,

我有一个使用ACCESS作为数据库的程序.

在我的数据库中,我有一个表,其中的同一行设置为:日期/时间,数据库中没有格式.

我想通过数据时间选择器在数据库中保存日期和时间.

从datatimepicker中检索值时,它仅将日期保存在我的数据库中,但是我需要时间.

如果我使用

Hello,

I have a program that use ACCESS as database.

In my database i have a table ho have same row set to: Date/Time with no format in the database.

I want to save a date and time in my database from a datatimepicker.

When a retrieve from my datatimepicker the value, it save in my database only the date, but i need and time.

If i use

DateTime saveDate = DateTime.Now;




这是鳕鱼要保存在我的数据库中.




This is the cod to save in my database.

private void btnAdaugaContract_Click(object sender, EventArgs e)
        {
            VerificareDateIntroduse();
            if (DateIntroduse == true)
            {
                try
                {
                    Program.Connection.CommandText = "INSERT INTO Contracts (ContractId, ClientId, CreateDate, StartDate, EndDate, Procent) values (@ContractId, @ClientId, @CreateDate, @StartDate, @EndDate, @Procent)";
                    Program.Connection.AddParameter("@ContractId", txtNumarContract.Text);
                    Program.Connection.AddParameter("@ClientId", ClientActiv.ClientID);
                    Program.Connection.AddParameter("@CreateDate", Convert.ToDateTime(dInceput.Value));
                    Program.Connection.AddParameter("@StartDate", Convert.ToDateTime(dInceput.Value));
                    Program.Connection.AddParameter("@EndDate", Convert.ToDateTime(dFinalizare.Value));
                    Program.Connection.AddParameter("@Procent", Convert.ToDouble(txtComision.Text.Replace('.', ',')));
                    Program.Connection.ExecuteNonQuery();

                    for (int i = 0; i < dataProduseAmanet.Rows.Count; i++)
                    {
                        Program.Connection.CommandText = "INSERT INTO ContractItems (ContractId, Name, Description, Payment, Quantity, QuantityUnit) values (@ContractId, @Name, @Description, @Payment, @Quantity, @QuantityUnit)";
                        Program.Connection.AddParameter("@ContractId", txtNumarContract.Text);
                        Program.Connection.AddParameter("@Name", dataProduseAmanet.Rows[i].Cells[0].Value.ToString());
                        Program.Connection.AddParameter("@Description", dataProduseAmanet.Rows[i].Cells[3].Value.ToString());
                        Program.Connection.AddParameter("@Payment", dataProduseAmanet.Rows[i].Cells[1].Value.ToString());
                        Program.Connection.AddParameter("@Quantity", dataProduseAmanet.Rows[i].Cells[2].Value.ToString());
                        Program.Connection.AddParameter("@QuantityUnit", dataProduseAmanet.Rows[i].Cells[4].Value.ToString());
                        Program.Connection.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                this.Close();
            }
        }




如果我替换




if i replace the

Program.Connection.AddParameter("@CreateDate", Convert.ToDateTime(dInceput.Value));



with

Program.Connection.AddParameter("@CreateDate", saveDate);





the

dInceput

是没有格式化的数据时间选择器.


saveDate 具有日期和时间,但是它给我一个错误:数据类型不匹配".

一些建议.

is the datatimepicker with no formating.


The saveDate have the date and time, but it gives me the error: "data type mismatch".

Some suggestions.
Tanks.

推荐答案

尝试使用DateTime.Now代替DateTime.New


Convert.ToDateTime(saveData.ToString("f"))


这篇关于DataTimePicker和访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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