简体   繁体   English

如何在 TXT 中打印数据库的每一行? C#

[英]How can I print each line of a database in a TXT? c#

I am doing a chat application with a MySQL database, my objective is load all the messages of my database in a TXT, I am using a foreach loop but it load word for word instead of line for line:我正在用 MySQL 数据库做一个聊天应用程序,我的目标是在 TXT 中加载我数据库的所有消息,我使用的是foreach 循环,但它逐字加载而不是逐行加载:

     while (reader.Read())
        {
            sqlcmd_answ = reader.GetString(0);
            foreach (var item in sqlcmd_answ.Split())
            {
                file.WriteLine(item);                   
            }

        }

It is my database: picture of my database这是我的数据库:我的数据库图片

And in my TXT i get that:在我的 TXT 中,我明白了:

 Hello Stack Overflow Community

I would like to get all these words in a line... Thanks!我想把所有这些词放在一起……谢谢!

You should print without loop.您应该无循环地打印。

Replace this:替换这个:

sqlcmd_answ = reader.GetString(0);
foreach (var item in sqlcmd_answ.Split())
{
    file.WriteLine(item);                   
}

by this:这样:

file.WriteLine(reader.GetString(0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM