Insert with datatime Postgresql and C #

0

I would like to ask a question, why does not the first date work?

  

Cmd.Parameters.AddWithValue ("@ dtUltAccess", "2017-01-01 01: 01: 01 + 09");

Error returning:

  

Error inserting user - Error: 42804: column "usu_dataacesso" is of   type timestamp without time zone but expression is of type

And that works fine

  

Cmd.Parameters.AddWithValue ("@ dtCadastro",   NpgsqlTypes.NpgsqlDateTime.Now);

 public void insere() 
                {
                    try
                    {
                        //Abre a conexao para insercao
                        abrirConexao();
                        Cmd = new NpgsqlCommand("insert into \"Cadastro\".\"usuario\" (usu_nome,usu_cargo,usu_cpf,usu_dataAcesso,usu_dataCadastro,usu_status,usu_login,usu_senha) values (@nome,@cargo,@cpf,@dtUltAcesso,@dtCadastro,@status,@login,@senha)", Con);
                        Cmd.Parameters.AddWithValue("@nome", "Flavio");
                        Cmd.Parameters.AddWithValue("@cargo","TI");
                        Cmd.Parameters.AddWithValue("@cpf", "0101010101");
                        Cmd.Parameters.AddWithValue("@dtUltAcesso", "2017-01-01 01:01:01+09");
                        Cmd.Parameters.AddWithValue("@dtCadastro", NpgsqlTypes.NpgsqlDateTime.Now);
                        Cmd.Parameters.AddWithValue("@status", "A");
                        Cmd.Parameters.AddWithValue("@login", "fla");
                        Cmd.Parameters.AddWithValue("@senha", "123");

                        Cmd.ExecuteNonQuery();

                        MessageBox.Show("Usuario Inserido com Sucesso");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Erro ao inserer usuario - Erro: " + ex.Message);

                    }

But I would like to pass a string with the date, how could I do that?

Thank you

    
asked by anonymous 17.03.2017 / 01:24

1 answer

1

When you pass the date between "" it goes as a string and not a date itself, the second case worked because a date was passed as a parameter! convert your string to date and everything will occur as expected

 Cmd.Parameters.AddWithValue("@dtUltAcesso",
                  DateTime.ParseExact("2017-01-01 01:01:01+09", "yyyy-MM-dd HH:mm:ss",
                                   System.Globalization.CultureInfo.InvariantCulture) );
    
17.03.2017 / 13:36