Insert with monthCalendar in MySql database and C #

1

I need to make a insert of date with monthCalendar in C# in database MySQL , except that the date format of MySQL is ( yyyy-MM-dd ) and I've already modified the code:

I changed the input mode (% with%)

cadast.Dataa=Convert.ToDateTime(monthCalendar1.SelectionStart.Date.ToString("yyyy-MM-dd"));

And I modified the class that does yyyy-MM-dd into the database:

  

insert

And it has the following error:

  

Incorrect datetime value error: '30 / 11/2016 00:00:00 'for function str_to_date.

I modified the string inserir = "INSERT INTO consultas (Dataa,Horario, cd_paciente, cro, cd_procedimento) values (str_to_date('" + cadast.Dataa + "','%Y-%m-%d'),'"+cadast.Horario+ "', '" + cadast.cd_paciente + "','" + cadast.cro + "' ,'" + cadast.cd_procedimento + "')"; again to:

  

insert

and I modified it in the field of string inserir = "INSERT INTO consultas (Dataa,Horario, cd_paciente, cro, cd_procedimento) values (str_to_date('" + cadast.Dataa + "','%Y-%m-%d %h:%i:%s %p'),'"+cadast.Horario+ "', '" + cadast.cd_paciente + "','" + cadast.cro + "' ,'" + cadast.cd_procedimento + "')"; to see if the problem was in it:

cadast.Dataa=
   Convert.ToDateTime(monthCalendar1.SelectionStart.Date.ToString("%Y-%m-%d %h:%i:%s%p"));

He gave the following error :

  

The string was not recognized as a valid DateTime. There is an unknown word that starts at index 0.

It also did not work already tried in all possible ways and so far I have not succeed, can anyone help me?

    
asked by anonymous 06.11.2016 / 12:51

2 answers

1

It is always recommended to use Parameterized Query when executing SQL commands that have parameters. This way you avoid security problems like SQL Injection and you do not have to worry about formatting data, avoiding problems like this you reported.

See just one example

using(SqlConnection conn = new SqlConnection(connectionString))
{
  conn.Open();
  using(SqlCommand cmd = new SqlCommand("INSERT INTO consultas (nome, data) values (@nome @data)", conn)) 
  {
    cmd.Parameters.AddWithValue("@nome", MySqlDbType.VarChar).Value = "Jon Snow";
    cmd.Parameters.AddWithValue("@data", MySqlDbType.DateTime).Value = DateTime.Now;
    cmd.ExecuteNonQuery();
  }
}

Note that we are passing an object of type DateTime to our command without doing any type of formatting.

    
06.11.2016 / 13:06
0

Good evening,

I can fix this date problem and I'll post my code here if anyone needs help in the future.

The solution was: I continued with the Dataa field in the Mysql database as date and in programming I left the date as a string and changed the date entry mode in the monthCalendar like this:

//Deixei como string, e mudei o modo de entrada da data no monthCalendar
       cadast.Dataa = monthCalendar1.SelectionStart.Date.ToString("yyyy-MM-dd");

The entire Code looks like this:

//classe class cadas_consulta {

//Deixei a data como string
    private string nDataa;



    public string Dataa
    {

        get { return nDataa; }
        set { nDataa = value; }
    }

//classe class dal_consulta { private MySqlConnection conexao;

     public void cadastro(cadas_consulta cadast)
     {
         string caminho = "SERVER=localhost;DATABASE=odonto_system;UID=root;PASSWORD=;";

         try
         {

             conexao = new MySqlConnection(caminho);
             conexao.Open();

             string inserir = "INSERT INTO consultas (Dataa,Horario, cd_paciente, cro, cd_procedimento) values ('" + cadast.Dataa + "','"+cadast.Horario+ "', '" + cadast.cd_paciente + "','" + cadast.cro + "' ,'" + cadast.cd_procedimento + "')";



             MySqlCommand comandos = new MySqlCommand(inserir, conexao);

             comandos.ExecuteNonQuery();
             MessageBox.Show("Consulta Cadastrada com Sucesso!");
             conexao.Close();

         }

         catch (Exception ex)
         {

             throw new Exception("Erro de comandos" + ex.Message);
         }
     }
}

//inserir no botão private void button1_Click(object sender, EventArgs e) {

        cadas_consulta cadast = new cadas_consulta();
        dal_consulta daa = new dal_consulta();

//Deixei como string também, e mudei o modo de entrada da data para padrão do banco MySql cadast.Dataa = monthCalendar1.SelectionStart.Date.ToString("yyyy-MM-dd");

        daa.cadastro(cadast);


    }'

Thanks to all who helped, because your answers help us to study more and search.

Thank you!

    
09.11.2016 / 23:28