我想写一个sql查询到一个文件,但我只能在文本文件中写入一列查询。我如何添加更多列?C#将sql查询写入文件?
这是我的C#Windows表单代码:
SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'");
SqlCommand command = con.CreateCommand();
command.CommandText = "Select * from bestillinger";
con.Open();
SqlDataReader queryReader = command.ExecuteReader();
while (queryReader.Read())
{
StreamWriter file = new StreamWriter(@"C:\Users\Michael\Desktop\query.txt");
file.WriteLine(queryReader["ordrenr"]);
file.Close();
}
queryReader.Close();
con.Close();
它不会让我写:
file.WriteLine(queryReader["ordrenr"] + queryReader["user"]);
如果您收到异常请在您的问题中加入。 – juharr 2014-09-26 18:19:56
我建议研究'使用'关键字,并且不要在循环的每次迭代中重新创建流。 – Magus 2014-09-26 18:24:08
检查我的代码! :) – mybirthname 2014-09-26 18:54:31