Insert with Postgresql datatime and c#

Asked

Viewed 49 times

0

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

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

Error that returns:

Error inserting user - Error: 42804: column "usu_dataaccess" is of type timestamp without time zone but Expression is of type text

And this one works right

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’d like to pass a string with a date, how could I do that?

Thank you

  • Tried to pass the information to the bank with simple quotes ''?

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 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) );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.