zzzprojects / Bulk-Operations

C# SQL Bulk Operations | High-performance C# bulk insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL, and SQLite.
https://bulk-operations.net
142 stars 34 forks source link

How to update selected data with BulkUpdate? #4

Closed Anshem closed 7 years ago

Anshem commented 8 years ago

Hi, I have a question about bulk update.

Very simple example. I have this Table:

| ID | NAME | PASS | | 1 | UserA | PassA | | 2 | UserB | PassB |

I want to update for user A his name, and for user B the Pass. How can i do it with bulk?

If I set empty the Pass value for User A, bulk set it like null, and the same for user B with his name. This is a problem for me. How can i resolve it?

What I want to say is that I want to set into bulk only the values that I want to update. This is because of restrictions of my application.

Thanks in advance.

zzzprojects commented 8 years ago

Helo @Anshem,

It's possible,

But in order to answer this question, I must know how you determine if you need to update the name or the pass, or both?

Best Regards,

Jonathan

Anshem commented 8 years ago

For each entity within the dataset, we do know what fieldnames to update (string array within the entity), and we have the proper values in a second string array. After that, for each entity we are able to compound the UPDATE query to only change the values that we need to change (UPDATE [tablename] (---concat first array---) VALUES (---concat second array---)).

Thanks.

zzzprojects commented 8 years ago

Oh I see,

We support some kind of formula, but I believe in your situation, this will be a bad idea (way to much complex for this situation).

I recommend you instead to make the logic in your code and perform multiple Bulk Operations

var groups = dt.AsEnumerable().GroupBy(x => x["ColumnWithFields"].ToString());

foreach (var group in groups)
{
    List<DataRow> rows = group.Select(x => x).ToList();

    // Bulk Operations
    // ... Add the code needed to perform the bulk operations...
}

This way, it will be way more simpler and readable.

Let me know if that could answer to your question or you are looking for a better solution.

Best Regards,

Jonathan

JonathanMagnan commented 7 years ago

Hello @Anshem ,

Was you satisfied with the answer? Can we close this issue?

Best Regards,

Jonathan

Anshem commented 7 years ago

Yes, thank you.

JonathanMagnan commented 7 years ago

Closing Comment: Answer confirmed