Correct DateTime format in SQL Server CE?

Viewed 17107

I have a C# DateTime class and wanted to know how I need to format it in a SQL Server CE query to insert it into the database, I was hoping to have both the date and time inserted in. Currently when I try variations thereof I get invalid format exceptions.

Current format I'm using is: dd/MM/yyyy, was hoping to do something like dd/MM/yyyy hh:mm:ss.

The way I'm trying to do the insert is like so:

 ( ( DateTime )_Value ).ToString( "dd/MM/yyyy hh:mm:ss" )

Obviously hh:mm:ss isn't working, if that isn't there dd/MM/yyyy executes successfully in the query.

I've tried a few formats including what I've found on google but none have worked so far...

5 Answers
private void button1_Click(object sender, EventArgs e)
{
    var cnn1 ="";//connection string 
    SqlCeConnection cnn = new SqlCeConnection(cnn1);   
    datetime dt4 = DateTime.Today.Date.ToString("yyyyMMdd").trim();//format 
    var qry ="insert into tbl_test(User_Id, DateOfJoin)values (11,'" + dt4 + "')";
   
    cmd = new SqlCeCommand(qry, cnn);
   
    try
    {
        dr = cmd.ExecuteReader();
    }
    catch (Exception ex)
    {
        string sr = ex.Message;
        throw;
    }
}

Above code worked for me.

1

Related