Hi ,
how generate data 500MB more second file generate in C# using data table.
Regards,
Pratik
Hi ,
how generate data 500MB more second file generate in C# using data table.
Regards,
Pratik
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Aug 14, 2024, 10:12 AM EDT
You will need to serialize the
DataTableinto a file and check the file size as you write. If it exceeds 500 MB, you should close the current file and start a new one. Also you can use aStreamWriterto write to files. Keep track of the file size and create new files as needed.using System; using System.Data; using System.IO; using System.Text; class Program { static void Main() { // Your DataTable with data DataTable dataTable = GetYourDataTable(); // Directory to save files string directoryPath = @"C:\YourDirectoryPath\"; string fileNamePrefix = "DataFile"; int maxFileSizeInMB = 500; int maxFileSizeInBytes = maxFileSizeInMB * 1024 * 1024; int fileCounter = 1; string currentFileName = Path.Combine(directoryPath, $"{fileNamePrefix}_{fileCounter}.txt"); using (StreamWriter writer = new StreamWriter(currentFileName, false, Encoding.UTF8)) { foreach (DataRow row in dataTable.Rows) { StringBuilder line = new StringBuilder(); // Convert the row to a string (you can customize the format) for (int i = 0; i maxFileSizeInBytes) { writer.Flush(); writer.Close(); // Create a new file fileCounter++; currentFileName = Path.Combine(directoryPath, $"{fileNamePrefix}_{fileCounter}.txt"); writer = new StreamWriter(currentFileName, false, Encoding.UTF8); } } } Console.WriteLine("Files generated successfully."); } static DataTable GetYourDataTable() { // Create and populate your DataTable here DataTable dt = new DataTable(); dt.Columns.Add("Column1"); dt.Columns.Add("Column2"); dt.Columns.Add("Column3"); for (int i = 0; i < 1000000; i++) { dt.Rows.Add("Data1_" + i, "Data2_" + i, "Data3_" + i); } return dt; } }