0

Este es mi codigo pero me da un error de convertir de fecha a hora , en mi base de datos esta de forma datetime no entiendo por que el error, espero me puedan ayudar, de antemano muchas gracias

            string nombre = textBox2.Text;
            string ap_paterno = textBox3.Text;
            string ap_materno = textBox4.Text;
            string velocidad = comboBox1.Text;
            DateTime fecha_inicio = dateTimePicker1.Value.Date;
            string calle = textBox5.Text;
            string direccion_ip = textBox6.Text;
            DateTime fecha_nac = dateTimePicker2.Value.Date;
            string correo = textBox8.Text;
            string estado = comboBox2.Text;

            SqlConnection con = new SqlConnection("Data Source=.;Initial catalog=wirilespacos;Integrated Security=True");
            con.Open();
            string textocmd = "insert into clientes_wisp3" +
                "(Matricula,Nombre,Ap_Paterno,Ap_Materno,Velocidad,Fecha_Inicio,Calle,estado,direccion_ip," +
                "fecha_nac,numero_tel,correo,numero_tel_alter)" +
                "values ('" + matricula + "','" + nombre + "','" + ap_paterno + "','" + ap_materno + "' " +
                ",'" + velocidad + "','" + fecha_inicio + "','" + calle + "','" + estado + "'" +
                ",'" + direccion_ip + "','" + dateTimePicker2.Value + "','" + numero_tel + "','" + correo + "'" +
                ",'" + numero_tel_alter + "')";


            MessageBox.Show(textocmd);
            SqlCommand cmd = new SqlCommand(textocmd, con);
           
            cmd.ExecuteNonQuery();```
1
  • Antes de seguir por este camino, revisa como parametrizar tus llamadas a la base de datos. Tu código está abierto a inyección de sql y esa es una vulnerabilidad importante. el 23 nov. 2021 a las 16:11

1 respuesta 1

0

Intenta formateando la fecha en "yyyy-MM-dd hh:mm:ss", algo asi:

        string nombre = textBox2.Text;
        string ap_paterno = textBox3.Text;
        string ap_materno = textBox4.Text;
        string velocidad = comboBox1.Text;
        var fecha_inicio = dateTimePicker1.Value.ToString("yyyy-MM-dd hh:mm:ss");
        string calle = textBox5.Text;
        string direccion_ip = textBox6.Text;
        var fecha_nac = dateTimePicker2.Value.ToString("yyyy-MM-dd hh:mm:ss");
        string correo = textBox8.Text;
        string estado = comboBox2.Text;

        SqlConnection con = new SqlConnection("Data Source=.;Initial catalog=wirilespacos;Integrated Security=True");
        con.Open();
        string textocmd = "insert into clientes_wisp3" +
            "(Matricula,Nombre,Ap_Paterno,Ap_Materno,Velocidad,Fecha_Inicio,Calle,estado,direccion_ip," +
            "fecha_nac,numero_tel,correo,numero_tel_alter)" +
            "values ('" + matricula + "','" + nombre + "','" + ap_paterno + "','" + ap_materno + "' " +
            ",'" + velocidad + "','" + fecha_inicio + "','" + calle + "','" + estado + "'" +
            ",'" + direccion_ip + "','" + dateTimePicker2.Value + "','" + numero_tel + "','" + correo + "'" +
            ",'" + numero_tel_alter + "')";


        MessageBox.Show(textocmd);
        SqlCommand cmd = new SqlCommand(textocmd, con);
       
        cmd.ExecuteNonQuery();```
2
  • 1
    Gracias me funciono de maravilla, te lo agradezco el 8 dic. 2021 a las 16:22
  • Un placer poder ayudarte, ese es el formato default de fecha para sql sin importar el lenguaje o la region, al instalar puedes seleccionar la region del sql igual que en otros productos de microsoft, bueno pues sql es compatible con ANSI he ISO 8601 que son estandares puedes ver la documentacion en este link docs.microsoft.com/es-mx/sql/t-sql/data-types/… en caso que quieras saber mas al respecto, saludos el 9 dic. 2021 a las 18:06

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.