Answered Export Datatable Records to file csv

blondie63

New member
Joined
Oct 24, 2020
Messages
2
Programming Experience
Beginner
I'm using this code to get my rows:
DataRow[] rows = AccDbDataSet.Tables["q_attuali"].Select("Azienda = '" + CompanySelected + "'");

How can i save to .csv all rows with column names as first row ?

Thanks for help

Blondie
 
Welcome to the forums.

You won't be getting a direct answer to this question because the nature of your question is incredibly broad... We have a rule whereas it's one question per topic. Your question covers multiple areas. To get better answers, start by asking one question per topic and narrow down your problems that way. Like how to get data from x control, or how to convert data from one source to another, etc. See How To Ask Questions - The Smart Way - First you need to get the data, then you will likely want to convert it to csv, and then you will want to write the new data. All of which are different areas which make your question broad.

One way you could do it if you wanted is to use Powershell. I suggest some some simple searches to find sample examples to get you started. I'm a little snowed under to be throwing out code.
Alternatively you might find this helpful : Export DataTable To CSV In C#
 
Further to what @Sheepings said, one of the most important lessons that a developer can learn is to break their problem down into parts and then tackle each part individually. That's problem-solving 101 really - divide and conquer - but it certainly applies to software development. To start with, a CSV file is simply a text file. Do you know how to write to a text file in C#? If not, that's the first thing to research. Once you can do that, then you can think about how to get the text you want to write from your DataTable. Do you know how to get data from a DataTable? If not, research that. Once you do know, then you can think about how to format it in the manner you need. Do you know how to format text? If not, research that. Etc.

People seem to think that they should be able to write code for the exact combination of problems they have in front of them without understanding any of the subjects involved. They shouldn't. They should understand the subjects first. In order to do that, they need to understand what those subjects are. That's where the analysis part of the problem comes in. You need to analyse the problem first, then research. There's no rule that says that you can't practice and test various simpler things in an effort to build up to the full solution you need. In fact, there should be a rule that you have to. Many people - especially beginners - think that if they can't write all the code to do exactly what they need then they should come to a screaming halt until someone else gives them the exact code. That's not how it works. Break it down into parts, research and test the parts that you can and then ask only specific questions about those parts that you have tried but failed to solve for yourself, then put all the partial solutions together to solve the original problem.

That is what software development is. That's what we all do on the complex enterprise applications we work on and the sooner that a beginner learns that, the sooner they become the best developer they can be. Very few people understand this at first, so I'm not necessarily criticising you for not knowing either. Now that you do know though, that's what you should strive to do. We will be more than happy to help with the parts that you can't work out, but we would like to see you work out what those parts actually are.
 
Thank you very much and I apologize for the request not formulated well, I resolved anyway with the link that @Sheepings sent me.
Actually I had found similar examples but I was hoping there was some shorter code .. I saw this CsvHelper but I was unable to use it completely.
 
That link looks just as promising, however, you will need to combine the link I gave you with this new library. It is also documented fairly reasonably : CsvHelper

I will meanwhile mark this topic answered.
 
Back
Top Bottom