using (SqlConnection con = new SqlConnection("server=.;database=TestDataBase;user=sa;pwd=123"))
{
using (SqlCommand cmd = new SqlCommand("select * from dbo.grade", con))
{
using (FileStream fs = new FileStream(@"d:\temp.txt",FileMode.Create,FileAccess.ReadWrite))
{
con.Open();
cmd.CommandType = CommandType.Text;
SqlDataReader reader = cmd.ExecuteReader();
using (TextWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
while (reader.Read())
{
string data = reader["username"].ToString() + "--------------" + reader["score"].ToString();
sw.WriteLine(data);
}
}
}
}
} |