Click here to Skip to main content
15,938,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi sir ,
I am uday

I am developing application in c sharp win form
I want emails from email table

I am retriving it from table and binding it in datatable

My datatable contains 1,00,000 emails.
And i want to store it on text file format.

please solve my problem

Thanks
Posted
Updated 23-Aug-11 23:15pm
v3
Comments
walterhevedeich 24-Aug-11 5:13am    
What is the data type of the email field on your database? Do you want to write 1 file for each email?

Hi, This is easy. I have given you a Example

C#
public static void WriteDataToFile(DataTable submittedDataTable, string submittedFilePath)
        {
            int i = 0;
            StreamWriter sw = null;

            sw = new StreamWriter(submittedFilePath, false);

            for (i = 0; i < submittedDataTable.Columns.Count - 1; i++)
            {

                sw.Write(submittedDataTable.Columns[i].ColumnName + ";");

            }
            sw.Write(submittedDataTable.Columns[i].ColumnName);
            sw.WriteLine();

            foreach (DataRow row in submittedDataTable.Rows)
            {
                object[] array = row.ItemArray;

                for (i = 0; i < array.Length - 1; i++)
                {
                    sw.Write(array[i].ToString() + ";");
                }
                sw.Write(array[i].ToString());
                sw.WriteLine();

            }

            sw.Close();
        }



As you have DataTable and FilePath Just Pass it to this Method and it will write the data to your file.

Thanks,
Rashim
 
Share this answer
 
Comments
udusat13 24-Aug-11 6:19am    
Yes its working for me
udusat13 24-Aug-11 6:19am    
Thanks
Md. Rashim Uddin 24-Aug-11 6:46am    
Please Vote it and mark it as Corret..Please pleaase
vipin-bhayana-3 1-Dec-17 4:24am    
It is working fine but my requirement is that - want to string in double quotes.
Out of box suggestion. OSQL can help you.
http://msdn.microsoft.com/en-us/library/aa214012(v=sql.80).aspx[^]
 
Share this answer
 
Comments
udusat13 24-Aug-11 5:16am    
I am confuse.please give me solution
C#
DataTable table = // You data table values;

       var result = new StringBuilder(); 
        foreach (DataRow row in table.Rows)         
        {             
            for (int i = 0; i < table.Columns.Count; i++)             
            {                 
                result.Append(row[i].ToString());                 
                result.Append(i == table.Columns.Count - 1 ? "\n" : ",");             
            }
            result.AppendLine();
        }

        StreamWriter objWriter = new StreamWriter("C:\\test.txt", false);
        objWriter.WriteLine(result.ToString());
        objWriter.Close();

Hope this help!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900