user6648485 user6648485 - 1 year ago 113
C# Question

Passing DateTime.Now into Access database

I am facing a problem on passing the

Now into Access database:

oleDBCommand.CommandText =
"INSERT INTO tblData "([PIC], [Sampling Date]) "VALUES (@PIC, @SamplingDate)";

oleDBCommand.Parameters.Add(new OleDbParameter("@PIC", combobox1.Text));
oleDBCommand.Parameters.Add(new OleDbParameter("@SamplingDate", DateTime.Now));

I tried a lot of methods from the internet like using
, using
And none of it is working.

Note 1: Database setting
[Sampling Date] = Data Type: Date/Time (Format - Long Time)
, database was

Note 2: Below code was working but I prefer to using .parameters as it look much more organize and easy to manage.

oleDBCommand.CommandText =
"INSERT INTO tblData ([PIC], [Sampling Date]) " VALUES ('" + combobox1.Text + "', '" + DateTime.Now + "')";

Answer Source

You dont need to pass parameter when specifying current date.

Let the ms access sql query handle it, you need to replace @SamplingDate parameter to Date() for example

cmd.CommandText = "INSERT INTO tblData ([PIC], [Sampling Date]) VALUES (@PIC, Date())";

Here is the best explanation Insert today's date

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download