Skip to content Skip to sidebar Skip to footer

How To Insert Datetime.now Values Into Datetime Type Field In Sql Database

my code is : SqlConnection cn = new SqlConnection('Data Source=.;Initial Catalog=haftehbazardb;Integrated Security=True '); SqlCommand cmd = cn.CreateCommand(); cmd.C

Solution 1:

You should be using parameterized queries.

    cmd.CommandText = "Insert into Table_test(date1,date2) Values(@Now,@21DaysLater)";
    cmd.Parameters.AddWithValue("@Now", DateTime.Now);
    cmd.Parameters.AddWithValue("@21DaysLater", DateTime.Now.AddDays(21));

It would be a good idea to insert all of your values, not just DateTime using parameters. Don't use string concatenation to write SQL.

Post a Comment for "How To Insert Datetime.now Values Into Datetime Type Field In Sql Database"