Insert data into the database via a C#Ridview dataset

Asked

Viewed 2,976 times

1

I have a form in C#:

Formulário

On this form there is a textbox where I enter the day of the week and a datagrid where I type in the hours.

I put a column call time, with DataGridViewCellStyle { Format=t } manual. But the mask __:__ is not appearing at runtime. I am using the following loop to save.

void INSERIRHORARIO()
{
    //Instância da conexão onde passo a
    //ConnectionString
    var conn = new MySqlConnection("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 for registering schedules. How to loop to take the connection from my class and send the data to the time class?

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; } 
    } 
}
  • 1

    If you put the class connection and class to register time I can help you, but the code above has a fruit salad, because it opens with the conn In the middle of the code there’s a conexao, must be wrong something, well as I said make the classes available!

  • Time Clesse 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 Time { get { Return time; } set { time = value; } } public int Qtdvagas { get { Return qtdVagas; } set { qtdVagas = value; } }

  • Place by editing your question: http://answall.com/posts/28827/edit

  • The connection class doesn’t fit here. But it does connect to the bank. kkkk

  • 1

    Fabricio gives yes look there I edited and put the class dia_hora.

  • Maria Bem, the class of dia_horario the class that makes the persistence of information in the bank. As I put above. I have an automatic Insert it takes the attributes and automatic values of the classes so I have to pass the data there. The kind that the code is on top he’s recording in the bank, but he thought if I have to change the server path, where the code is, I’ll have to change it. so oo.

  • Just a tip friend Fabricio, when I saw it here dia_hora : ModeloCrud has already given me subsidies to say that your model may be functional, but, out of the standard and are little information for anyone to answer, sorry like the stimulus, but, I had to make a constructive criticism... Sorry again!

  • quiet, I really need how to make this information pass through the class without making a direct connection. As it is up there

  • Ala maria again. Thanks for the tip I’ll let my solution in baico really worth

Show 4 more comments

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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.