How to update a table using mysql c# and a method?

Asked

Viewed 186 times

0

You can use a method or class that uses table fields to update this table?

For example: update record field 1 with f(record field 2 1), record field 1 with f(record field 2), etc...

reg|campo1|campo2
1    f(x)   x 
2    f(y)   y

Code would be something like:

string conectaBanco = "server = localhost; port = 3306; User Id = root; 
database=mysql; password=";
MySqlConnection objcon = new MySqlConnection(conectaBanco);
objcon.Open();

MySqlCommand objCmd = new MySqlCommand("tabela_A set campo1=?", objcon);
objCmd.Parameters.Add("@campo1", MySqlDbType.VarChar, 20).Value = 
metodo_qualquer(param1);
objCmd.ExecuteNonQuery();

Whereas the metodo_qualquer() is executed with each record that is updated

A more didactic example: A field of the Datanote table (field1) contains the responses of one student to a test (ACDEB) and in another (field2) there is the template (CCDAE). Camp 3 will contain the number of hits. Then I need to update the field3 with the information of the number of hits that will be the result of the treatment of fields 1 and 2. For this there is a method (calcAcertos) that, given field1 and field2, calculates the number of hits treating the strings "ACDEB" and "CCDAE". It is therefore necessary to: update Datanota set campo3=calcAcertos(campo1, campo2)

Field 1 = ACDEB Field 2 = CCDAE Correct answers = 2 Campo3 = 2

  • 1

    It’s kind of hard to understand your question... especially in the example, would have to make more clear?

  • You can use the same field in update yes: update tabela_A set campo1= campo2 + campo2 where reg = ?; (example summing the fields, will know qq is this f(x))

No answers

Browser other questions tagged

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