Quantcast
Channel: sql-server – Tech ABC to XYZ
Viewing all articles
Browse latest Browse all 30

Inserting date in database [ANSWERED]

$
0
0

Inserting date in database

Asked By: user3163265
Originally Asked On: 2014-01-05 19:17:27
Asked Via: stackoverflow

datofreg is of type datetime

my command is

SqlCommand cmd = new SqlCommand("insert into stud(datofreg) values('"+DateTime.Now+"'",conn);

when i execute it says Incorrect syntax near ‘1/6/2014 12:45:17 AM’.

He received 1 answers
eventually accepting:

Soner Gönül’s answer to

Inserting date in database

Your problem is you are missing ) at the end of your query but don’t use this way..

"insert into stud(datofreg) values('" + DateTime.Now + "')"
                                                        ^^here

You should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

For example;

SqlCommand cmd = new SqlCommand("insert into stud(datofreg) values(@date)", conn);
cmd.Parameters.AddWithValue("@date", DateTime.Now);
cmd.ExecuteNonQuery();

If the selected answer did not help you out, the other answers might!

All Answers For: Inserting date in database

Soner Gönül’s answer to

Inserting date in database

Your problem is you are missing ) at the end of your query but don’t use this way..

"insert into stud(datofreg) values('" + DateTime.Now + "')"
                                                        ^^here

You should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

For example;

SqlCommand cmd = new SqlCommand("insert into stud(datofreg) values(@date)", conn);
cmd.Parameters.AddWithValue("@date", DateTime.Now);
cmd.ExecuteNonQuery();

Of course, you should really check out the original question.

The post Inserting date in database [ANSWERED] appeared first on Tech ABC to XYZ.


Viewing all articles
Browse latest Browse all 30

Trending Articles