简体   繁体   中英

How update all rows in that case : MYSQL + C# dynamic code that calculate different value for each row

(use mysql + C# .net connector)

The Case:

table1 input:

item_id| long_name               |short_name
---------------------------------------------
56763  |MY NEW YORK 34 the great |
76878  |in the paris was really..|

table1 ouput:

item_id| long_name               |short_name
---------------------------------------------
56763  |MY NEW YORK 34 the great |NEW YORK
76878  |in the paris was really..|paris

I calculate the short_name by C# code

How can it done with UPDATE instead TRUNCATE+INSERT INTO (real table have more then 100,000 rows that have dynamic values separate for each row)?

You can load the data in a DataTable , update the columns with your C# function and then update the database like:

SqlDataAdapter.Update(Dataset.Tables["table1"]);

This will send an UPDATE statement for every row though. You should think a bit about your architecture. What do you want to achieve? Is it possible to put the logic in a trigger instead of C# code? Can you reduce the number of rows you have to update by putting another condition (recently added, etc.)?

I suppose you have some algorithm which returns the "short name" from the "long name".
Therefore, you need to implement it in SQL, and execute it on the MySQL server.

If you wrote your algorithm in C# - you can make the client program (the C# code) do the calculations, and then, using an UPDATE query - update the changes to the database. But for large databases, this is very inefficient and slow (downloading 100,000 rows it quite a bit) - so you should think of writing in SQL instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM