Question How to process MySQL results in C# - compared to PHP

MichaelTalio

New member
Joined
Jan 21, 2023
Messages
1
Programming Experience
1-3
Hello.

I hope you can help with a beginner question, where I didnt find the answer in the web yet.

I'm working with quite big data from a MySQL DB.

In PHP I fetch the needed data and push in into an multidimensional array.
Then I process this data in the array with all help of all the functions PHP provides (I calculate things, update data in the array, use array_search etc.).

I wonderr, if you can help me to find the best practice in C# for me?

What I have read so far is, that I should not use c# Array. I should use List instead or a class.

But which of these options would be similar to my known "work flow" how to process MySQL data?
I need to add data to this "list" from other MySQL tables, calculate things, update values in it, drop unneeded data.

Any help to find the right way I could go on would be very nice.

Best regards,
Michael
 
It's hard to say for sure because your description is very general but you may well just be able to use a DataTable. Use a data adapter to retrieve data from the database into a DataTable, modify the data as required and then save changes back to the database using the same data adapter. You might also consider using Entity Framework or some other ORM to load the data into a list of a dedicated type, or you could use a data reader and load the data into a list of a dedicated type yourself. You probably wouldn't use an array in this specific scenario, although the system will almost certainly use arrays under the hood. There's nothing wrong with using arrays directly in appropriate situations though. Some people seem to think that you should always use a List<T> in preference to an array but that's just not the case. You can do, but if you're not using the extra functionality the collection provides then there's no point using one.
 
We need to know what you're going to do. An entire book could be written in answer to "how do I download data from a database and work with it?"

Sounds like you might be doing too much processing on the client though
 
It's hard to say for sure because your description is very general but you may well just be able to use a DataTable. Use a data adapter to retrieve data from the database into a DataTable, modify the data as required and then save changes back to the database using the same data adapter. You might also consider using Entity Framework or some other ORM to load the data into a list of a dedicated type, or you could use a data reader and load the data into a list of a dedicated type yourself. You probably wouldn't use an array in this specific scenario, although the system will almost certainly use arrays under the hood. There's nothing wrong with using arrays directly in appropriate situations though. Some people seem to think that you should always use a List<T> in preference to an array but that's just not the case. You can do, but if you're not using the extra functionality the collection provides then there's no point using one.

Agreed - Dapper is an extremely easy ORM to implement if that is the route you are considering.
 
In your PHP code, what are you doing with the presumably 2-dimensional array? If you are only ever processing on row at a time within that 2-dimensional array, then yes, the recommendation of using a list of objects would definitely make sense, or even better why not just process the data one row at a time straight from the database? Why even bother loading everything into memory?

As an aside, how was the logic in your "workflow" put together? Was it just a pure transliteration of some manual process into code without an analyst or programmer trying to analyze and get to essence of what the set of operations is actually trying to do and eventually get to minimal set of work needed? Was factor analysis performed to figure out what work needs to be done with all the data loaded in memory vs what can be done on only a single row or single column basis?
 
Hello.

I hope you can help with a beginner question, where I didnt find the answer in the web yet.

I'm working with quite big data from a MySQL DB.

In PHP I fetch the needed data and push in into an multidimensional array.
Then I process this data in the array with all help of all the functions PHP provides (I calculate things, update data in the array, use array_search etc.).

I wonderr, if you can help me to find the best practice in C# for me?

What I have read so far is, that I should not use c# Array. I should use List instead or a class.

But which of these options would be similar to my known "work flow" how to process MySQL data?
I need to add data to this "list" from other MySQL tables, calculate things, update values in it, drop unneeded data.

Any help to find the right way I could go on would be very nice.

Best regards,
Michael

Hello this is Gulshan Negi
Well, in C#, using a list would be a good choice for working with dynamic arrays of data, as it allows you to add, remove, and modify elements easily. However, if you need to perform more complex operations on your data, such as sorting, grouping, or filtering, you may want to consider using a collection class that provides more functionality, such as ObservableCollection or HashSet.

Thanks
 
if you need to perform more complex operations on your data, such as sorting, grouping, or filtering, you may want to consider using a collection class that provides more functionality, such as ObservableCollection or HashSet.
Given that neither of those types provide grouping or filtering directly and neither provide sorting while the List<T> does, I'd say no, don't do that. The point of an ObservableCollection is that it provides change notifications, so it's good for data-binding and other scenarios where you ned to react to changes. The point of a HashSet is to force the items to be unique. If you don't need those things, don't use those types.
 
Hello.

I hope you can help with a beginner question, where I didnt find the answer in the web yet.

I'm working with quite big data from a MySQL DB.

In PHP I fetch the needed data and push in into an multidimensional array.
Then I process this data in the array with all help of all the functions PHP provides (I calculate things, update data in the array, use array_search etc.).

I wonderr, if you can help me to find the best practice in C# for me?

What I have read so far is, that I should not use c# Array. I should use List instead or a class.

But which of these options would be similar to my known "work flow" how to process MySQL data?
I need to add data to this "list" from other MySQL tables, calculate things, update values in it, drop unneeded data.

Any help to find the right way I could go on would be very nice.

Best regards,
Michael

PHP array vs. C# List

A PHP array is a data structure that can store any type of data, including strings, integers, floats, arrays, and objects. A C# List is a generic collection class that can store a collection of objects of the same type.

In general, it is recommended to use a C# List instead of an array for the following reasons:

  • Lists are more flexible and can be resized dynamically.
  • Lists provide a number of methods that can be used to manipulate the data in the collection, such as Add(), Remove(), and Sort().
  • Lists are type-safe, which means that you can only add objects of the same type to the list. This helps to prevent errors.
Processing MySQL data in C#

To process MySQL data in C#, you can use the following steps:

  1. Connect to the MySQL database using the MySql.Data.dll assembly.
  2. Create a MySqlCommand object and specify the SQL query to execute.
  3. Execute the query using the MySqlCommand object.
  4. Read the results of the query using the MySqlDataReader object.
  5. Iterate through the results of the query and process the data.
  6. Close the connection to the database.
 
Back
Top Bottom