如何在文本文件中写入数据表结果 [英] How to write datatable result in textfile

查看:123
本文介绍了如何在文本文件中写入数据表结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,
我现在是

我正在以C Sharp Win形式开发应用程序
我想要电子邮件表中的电子邮件

我正在从表中检索它并将其绑定到datatable

我的数据表包含1,00,000封电子邮件.
我想将其存储为文本文件格式.

请解决我的问题

谢谢,

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

推荐答案

这很容易.我给你举个例子

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

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();
        }




当您具有DataTable和FilePath时,只需将其传递给此方法,它将把数据写入您的文件中.

谢谢,
Rashim




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

Thanks,
Rashim


开箱即用的建议. OSQL可以为您提供帮助.
http://msdn.microsoft.com/en-us/library/aa214012 (v = sql.80).aspx [
Out of box suggestion. OSQL can help you.
http://msdn.microsoft.com/en-us/library/aa214012(v=sql.80).aspx[^]


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!


这篇关于如何在文本文件中写入数据表结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆