Updating a datagrid with connection to mysql

1

I can not update a table from a database linked to a datagrid. When I change the data in the cells of the datagrid, although they appear changed during the debug, at the end the table is not updated. Does anyone know what's missing in the code?

private static OdbcConnection CreateConnection()
{
    return new OdbcConnection("driver= {MySQL ODBC 5.1 Driver};server=192.168.20.29; database=yyyy; uid=esta; password=1234; option = 3 ");
}
using (OdbcConnection connection = CreateConnection())
{
string CmdText = "update lojas set Bloqueado =@bloqueador, DataFim = @DataFim, Nome = @Nome where Id =@Id";

OdbcCommand cm = new OdbcCommand(CmdText, connection);
cm.CommandType = CommandType.Text;
connection.Open();

cm.Parameters.AddWithValue("@Id", grid_lic.CurrentRow.Cells[0].Value);
cm.Parameters.AddWithValue("@bloqueador",grid_lic.CurrentRow.Cells[3].Value);
cm.Parameters.AddWithValue("@DataFim",grid_lic.CurrentRow.Cells[4].Value);
cm.Parameters.AddWithValue("@Nome",grid_lic.CurrentRow.Cells[6].Value);

cm.ExecuteNonQuery();

}

    
asked by anonymous 16.12.2015 / 11:22

1 answer

1

I advise you to use Mysql.data you can get the dll , using it will become easier, follows example of connection with the bank and update of the same one:

bdConn = new MySqlConnection("Persist Security Info=False;server=SERVIDOR;database=BANCO;uid=LOGIN;pwd=SENHA");
try
{
    bdConn.Open();
    MySqlCommand updateCommand = new MySqlCommand("UPDATE lojas SET Bloqueado =@bloqueador, DataFim = @DataFim, Nome = @Nome WHERE Id =@Id", bdConn);
    updateCommand.Parameters.AddWithValue("@Id", grid_lic.CurrentRow.Cells[0].Value);
    updateCommand.Parameters.AddWithValue("@bloqueador",grid_lic.CurrentRow.Cells[3].Value);
    updateCommand.Parameters.AddWithValue("@DataFim",grid_lic.CurrentRow.Cells[4].Value);
    updateCommand.Parameters.AddWithValue("@Nome",grid_lic.CurrentRow.Cells[6].Value);
    updateCommand.ExecuteNonQuery();
}
catch (MySqlException ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    //Fechar conexão aberta
    bdConn.Close();
}

It is advisable to open and close the connection for all the queries you will perform (it is also advisable to make as many queries as you can with this connection to save time in the process of opening and closing the connection with the bank).

    
16.12.2015 / 14:13