Statement in sql for a datetime

3

I have this fragment of cogigo

public List<byte[]> selectAlarmas1(int numero)
        {
            try
            {
                cn = new SqlConnection("Data Source = PRUEBA; Initial Catalog = FUCS; Persist Security Info = True; User ID = admin; Password = 1234");
                cn.Open();
                Console.Write("conectado bien");
            }
            catch (Exception e)
            {
                Console.Write("no se conecto" + e);
            }
            byte[] Datos = null;
            List<byte[]> numeros = new List<byte[]>();
            try
            {
                if (numero == 1)
                {
                    String cama = TextBox1.Text;
                    String Hora = HoraGrafica1.Text;
                    cmd = new SqlCommand("Select ECG1 From dbo.Alarm where PatientiID=" + cama+ "AND OccurTime='"+Hora+"';", cn);
                    dr = cmd.ExecuteReader();
                }

I in the variable that says Hora I assign something this way "2009/10/10 09:23" and I need you to bring me all the data from the database that are between 09:23: 00 until 09:23:59 but with this sentence it only takes one I would like to know how I should do the sentence to bring them all Thanks

    
asked by Sebastian Mateus Villegas 17.10.2016 в 22:05
source

1 answer

7

I suggest you consider parameterizing your values in the request to the database, as well as restructuring your try catch. Here is an example with the use of using so that you have the object of your connection at the end of use it:

public List<byte[]> selectAlarmas1(int numero)
    {

        string connectionString="Data Source = PRUEBA; Initial Catalog = FUCS; Persist Security Info = True; User ID = admin; Password = 1234";
        byte[] Datos = null;
        List<byte[]> numeros = new List<byte[]>();
        if (numero == 1)
        {
            string cama = TextBox1.Text;
            string Hora = HoraGrafica1.Text;
            string query= "Select ECG1 From dbo.Alarm where PatientiID=@cama AND OccurTime between @horaInicio and @horaFin ;";
        }
        using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.Add("@cama", SqlDbType.VarChar);
                command.Parameters.Add("@horaInicio", SqlDbType.DateTime);
                command.Parameters.Add("@horaFin", SqlDbType.DateTime);
                command.Parameters["@cama"].Value = cama;
                command.Parameters["@horaInicio"].Value = DateTime.Parse(HoraGrafica1.Text); //aquí tomaría el segundo 0
                command.Parameters["@horaFin"].Value =  DateTime.Parse(HoraGrafica1.Text).AddSeconds(59);//Le agregué 59 segundos

                try
                {
                    connection.Open();
                    dr = cmd.ExecuteReader();
                }
                catch (SqlException ex)
                {
                    Console.Write("no se conecto" + ex.Message);
                }
            }
    }

You must consider that your database is also being saved with seconds, otherwise the previous with the 2nd line of time is not necessary.

    
answered by 17.10.2016 / 22:40
source