Insert data into the database via a dataGridview in C #

1

I have a form in C #:

InthisformthereistextboxwhereItypethedayoftheweekandadatagridwhereItypethehours.

Iputacolumncalledtime,withDataGridViewCellStyle{Format=t}manual.Butthe__:__maskisnotappearingatruntime.Iamusingthefollowinglooptosave.

voidINSERIRHORARIO(){//Instânciadaconexãoondepassoa//ConnectionStringvarconn=newMySqlConnection("Persist Security Info=False;Server=10.1.1.50;database=sistema_escola;uid=alfa;pwd=;");
    // //sql que será executado na tabela cliente
    var sql = "INSERT INTO dia_hora (dia, horario, qtdVagas) " +
              "VALUES (@dia, @horario, @qtdVagas)";
    //instância do comando onde passo
    //o sql e a conexão como parâmetro
    var cmd = new MySqlCommand(sql, conn);
    //abro a conexão
    strSelect = "SELECT SUM(qtdVagas) AS Vagas FROM laboratorio";
    DataTable tabela;
    tabela = conexao.ExecultarSelect(strSelect);
    conn.Open();

    //percorro o DataGridView
    for (int i = 0; i < dGVHorario.Rows.Count - 1; i++)
    {
        //limpo os parâmetros
        cmd.Parameters.Clear();
        //crio os parâmetro do comando
        //e passo as linhas do dgvClientes para eles
        //onde a célula indica a coluna do dgv
        cmd.Parameters.AddWithValue("@dia", txtDiadaSemana.Text);
        //dGVHorario.Rows[i].Cells[0].Value);
        cmd.Parameters.AddWithValue("@horario", dGVHorario.Rows[i].Cells[0].Value);
        cmd.Parameters.AddWithValue("@qtdVagas", tabela.Rows[0]["Vagas"].ToString());

        //executo o comando
        cmd.ExecuteNonQuery();
    }
    //Fecho conexão
    conn.Close();

But, I'm working with object orientation, so I already have a class for connection, and a class to register schedules. Is there a way to loop to get the connection from my class and send the data to the time class?

Day_day class

public class dia_hora : ModeloCrud { 
    private string dia; 
    private string horario; 
    private int qtdVagas; 
    public string Dia 
    { 
        get { return dia; } 
        set { dia = value; } 
    } 
    public string Horario 
    { 
        get { return horario; } 
        set { horario = value; } 
    }       
    public int QtdVagas 
    { 
        get { return qtdVagas; } 
        set { qtdVagas = value; } 
    } 
}
    
asked by anonymous 13.08.2014 / 14:34

1 answer

1
strSelect = "SELECT SUM(qtdVagas) AS Vagas FROM laboratorio";
DataTable tabela;
tabela = conexao.ExecutarSelect(strSelect);
for (int i = 0; i < dgvhorario.Rows.Count - 1; i++)
{
     diahora.Dia = txtDiadaSemana.Text;
     diahora.Horario = Convert.ToString(dgvhorario.Rows[i].Cells[0].Value);
     diahora.QtdVagas =Convert.ToInt32(tabela.Rows[0]["Vagas"].ToString());
     diahora.SALVAR();
}
    
13.08.2014 / 20:16