【问题标题】:How can I print each line of a database in a TXT? c#如何在 TEXT 中打印数据库的每一行? C#
【发布时间】:2017-01-15 15:54:41
【问题描述】:

我正在使用 MySQL 数据库做一个聊天应用程序,我的目标是在 TXT 中加载我的数据库的所有消息,我正在使用 foreach 循环,但它是逐字加载而不是逐行加载换行:

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

        }

这是我的数据库: picture of my database

在我的 TXT 中,我明白了:

Hello
Stack
Overflow
Community

我想把所有这些词排成一行…… 谢谢!

【问题讨论】:

    标签: c# mysql for-loop


    【解决方案1】:

    您应该在没有循环的情况下打印。

    替换这个:

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

    通过这个:

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

    【讨论】:

    猜你喜欢
    • 2014-03-28
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多