I am new to .net core application .I need to import 4 different tables value from Sql server into 4 separate CSV files using windows Console app .net core
Loading
I am new to .net core application .I need to import 4 different tables value from Sql server into 4 separate CSV files using windows Console app .net core
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Naimish MakwanaPosted Mar 9, 2023, 3:52 AM
Hello Meghna,
Try below code
Another approach is, you can directly use SSMS to export SQL Server data to a CSV file. Use below link:
https://solutioncenter.apexsql.com/how-to-export-sql-server-data-to-a-csv-file/
Thanks
Naimish Makwana
Tuhin PaulPosted Mar 9, 2023, 1:05 AM
Try the following steps:
Step 1: Add the necessary NuGet packages to your project: Microsoft.Data.SqlClient, CsvHelper.
Step 2: Establish a connection to the SQL Server database using SqlConnection object and create a SqlCommand object to execute the SELECT statement to retrieve the data from the tables.
Step 3: Create a SqlDataReader object to retrieve the data returned from the SELECT statement.
Step 4: Create a CsvWriter object to write the data to a CSV file.
Step 5: Loop through the SqlDataReader object and write the data to the CsvWriter object.
Step 6: Save the CSV file and close the SqlDataReader object and the CsvWriter object.
Vishal YelvePosted Mar 8, 2023, 7:44 PM
Use ClosedXML