Datetime attribute error in C# saving to Mysql

Asked

Viewed 580 times

2

I am trying to enter in the Mysql database the date of birth of the user, in C# the attribute data_birth is of type DateTime and in Mysql the attribute data_nascimento is of type Date, to insert C# I’m doing so:

DateTime nascimento = Convert.ToDateTime(txNascimento.Text);
bDep.Data_nascimento = nascimento;

Obs:. bDep is the basic class of the dependent

And in the dependent’s data class I’m inserting thus:

public void insertDependente (BDependente bDep) {
      string sql = "INSERT INTO dependente(id_funcionario, nome, grau_parentesco, data_nascimento) ";
             sql += "VALUES ("+bDep.Funcionario.Id_funcionario+", '"+bDep.Nome+"', '"+bDep.Grau_parentesco+"', '"+bDep.Data_nascimento+"')";
            conn.update(sql);
        }

am selecting thus:

string sql = "SELECT id_dependente, id_funcionario, nome, grau_parentesco, data_nascimento ";
       sql += "FROM dependente";
                while (mdr.Read()) {
                    BDependente bDepe = new BDependente();
                    bDepe.Id_dependente      = mdr.GetInt16("id_dependente");
                    bDepe.Funcionario.Id_funcionario = mdr.GetInt16("id_funcionario");
                    bDepe.Nome               = mdr.GetString("nome");
                    bDepe.Grau_parentesco    = mdr.GetString("grau_parentesco");
                    bDepe.Data_nascimento    = mdr.GetDateTime("data_nascimento");
                    lDep.Add(bDepe);
                }

Every time I register a new addict at the bank it appears like this:

linhas do banco de dados

The first record was made in Mysql itself, and even so when I search in C# it brings the time and when I search in C# the other dependent, appears this other error:

Mensagem de erro Como aparece na aplicação

  • 1

    The error only occurs when the date is written wrong or happens on any date line?

  • Error happens when recording ! And when I make one SELECT at the bank

  • That’s not what I asked.

1 answer

4


The problem starts in the recording. That’s not how it’s done. It has a huge security problem in doing this in addition to generating this kind of problem.

From what I understand the error in reading occurs because the write failed.

I’d have to switch to something like this:

var sqlInsert = new SqlCommand(@"Insert into dependente (id_funcionario, nome, grau_parentesco, data_nascimento)
                    VALUES (@id_funcionario, @nome, @grau_parentesco, @data_nascimento)", conn);
sqlInsert.Parameters.AddWithValue("@id_funcionario", bDep.Funcionario.Id_funcionario);
sqlInsert.Parameters.AddWithValue("@nome", bDep.Nome);
sqlInsert.Parameters.AddWithValue("@grau_parentesco", bDep.Grau_parentesco);
sqlInsert.Parameters.AddWithValue("@data_nascimento", bDep.Data_nascimento.ToShortDateString());
sqlInsert.ExecuteNonQuery();

I put in the Github for future reference.

Then you will adapt to your need. And please never try to record this way again.

The ideal would be not to use variables with hungarian notation also.

I’m not a fan of the term attribute, I prefer field.

  • I wanted to know how I’m going to do it using a connection class:

  • public void Connecting() { string url = "server=localhost; database=atvfuncionaio; uid=root; password='; Convert zero datetime=True"; Conn = new Mysqlconnection(url); Conn. Open(); } public void disconnecting() { Conn.Dispose(); Conn.Close(); }

  • This is already another problem and only with this information I would not be able to say. It seems to me that there is no need to change anything in relation to this. Of course what this class seems to be a wrong code, but like I said, it’s another problem.

  • Insertion continues with error ! it entered the date 0000-00-00

  • That’s how you do it, I don’t know if there’s any other problem.

  • bDep.Data_nascimento = Convert.ToDateTime(txNascimento.Text); That’s how you insert ?

  • look at this comment above !

  • I think you are with another problem and what is in the question has no way of knowing. When it is so need to provide a [mcve]. But that would probably be another question.

  • Helped a lot ! :/

  • 1

    @Ikarosales the line sqlInsert.Parameters.AddWithValue("@data_nascimento", bDep.Data_nascimento.ToShortDateString()); the answer is wrong, because it depends on the current culture, whose format may be different from that accepted by Mysql. The correct is sqlInsert.Parameters.AddWithValue("@data_nascimento", bDep.Data_nascimento);, without the Toshortdatestring. This will cause the date parameter to be reported as the date type of fact (being converted internally by the driver to the date format of the database) and solve your problem.

Show 5 more comments

Browser other questions tagged

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