c# comparar una feha de inicio y una fecha final
Publicado por Fco Navarro (11 intervenciones) el 14/08/2018 16:30:29
c# comparar una feha de inicio y una fecha final y que se pueda agregar en sql server
Valora esta pregunta
0
private void button1_Click(object sender, EventArgs e)
{
SqlCommand comando = new SqlCommand ( "INSERT INTO fechaprueba(correctivo,hora_analisis,numero,fecha_registro,fecha_fin) " + "VALUES(@CO, @HA, @NU, @FR,@FF)", conexcion);
comando.Parameters.AddWithValue("@CO", txtcorre.Text);
comando.Parameters.AddWithValue("@HA", txtha.Text);
comando.Parameters.AddWithValue("@NU", txtnu.Text);
comando.Parameters.AddWithValue("@FR", fechainicial.Value);
comando.Parameters.AddWithValue("@FF", fechafin.Value);
conexcion.Open();
comando.ExecuteNonQuery();
conexcion.Close();
MessageBox.Show("SI");
}
///pseudo codigo --puede contener errores de sintaxis solo seguir la idea.
private void button1_Click(object sender, EventArgs e)
{
//validando fechas si fecha posterior es antes que fecha fin no se hace nada
if( ! validarFechas(fechainicial.Value, fechainicial.Value))
{
Messagebox.Show("La fecha final debe ser posterior a la fecha inicial");
return;
}
insertarRegistro()
}
private void insertarRegistro()
{
SqlCommand comando = new SqlCommand ( "INSERT INTO fechaprueba(correctivo,hora_analisis,numero,fecha_registro,fecha_fin) " + "VALUES(@CO, @HA, @NU, @FR,@FF)", conexcion);
comando.Parameters.AddWithValue("@CO", txtcorre.Text);
comando.Parameters.AddWithValue("@HA", txtha.Text);
comando.Parameters.AddWithValue("@NU", txtnu.Text);
comando.Parameters.AddWithValue("@FR", fechainicial.Value);
comando.Parameters.AddWithValue("@FF", fechafin.Value);
conexcion.Open();
comando.ExecuteNonQuery();
conexcion.Close();
MessageBox.Show("SI");
}
//validando que la fecha final sea mayor a la fecha inicial
private bool ValidarFechas(string fechaInicial, string FechaFinal)
{
DateTime fechaInicio= new DateTime(fechaIncial);
DateTime fechaFin= new DateTime(fechaFinal);
int result = DateTime.Compare(fechaInicio,fechaFin);
string relationship;
if (result < 0)//fechaInicio es anterior a fecha fin
return true;
else if (result == 0) //ambas son las mismas fechas
return false;
else //fecha fin es menor a fecha inicial
return false;
}