如何用C#将查询结果写入文件。例如(姓名工资)

如何用C#将查询结果写入文件。例如(姓名工资),c#,sql,C#,Sql,我需要在.doc文件中用C编写查询结果# 代码 SqlCommand command = new SqlCommand("SELECT Id,Name,Price FROM [Products] WHERE [Id]=@Id ", sqlConnection); While (await sqlReader.ReadAsync()){ listBox3.Items.Add(Convert.ToString(sqlReader["id"]) + " " + Convert.To

我需要在.doc文件中用C编写查询结果# 代码

SqlCommand command = new SqlCommand("SELECT Id,Name,Price FROM [Products] WHERE [Id]=@Id ", sqlConnection);
While (await sqlReader.ReadAsync()){
    listBox3.Items.Add(Convert.ToString(sqlReader["id"]) + "  " + 
    Convert.ToString(sqlReader["Name"]) + "  " + (1 + ((readSearch1 - f) * 1.2) / f));
}

您可以使用Docx库来执行此操作。


次要的一点:由于
被错误地大写,这甚至不能像编写的那样编译。这不是有效的C#或SQL。我没有看到任何关于.doc文件的内容。这不是向ListBox控件添加项的方法。
 // Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
    // Create a new Paragraph with the text "Hello World".
    Paragraph p = document.InsertParagraph("Hello World.");

    // Make this Paragraph flow right to left. Default is left to right.
    p.Direction = Direction.RightToLeft;

    // Save all changes made to this document.
    document.Save();
}