conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from t_import";
SqlDataReader sdr = cmd.ExecuteReader();
using (FileStream fs = File.Create(saveFileDialog1.FileName))
{
using (StreamWriter sw = new StreamWriter(fs))
{
while (sdr.Read())
{
string name = sdr.GetString(sdr.GetOrdinal("name"));
int age = sdr.GetInt32(sdr.GetOrdinal("age"));
sw.WriteLine("{0}-{1}", name, age);
}
}
}
}
===============================================
以上代码中感觉这句话不要也能行
using (FileStream fs = File.Create(saveFileDialog1.FileName))
只要把using (StreamWriter sw = new StreamWriter(fs))这句话改成
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
也行,
小弟深感迷惑,请高手指点迷津,感激不尽
FileStream包括读写操作; StreamReader 只是读操作;StreamWriter只是写操作;
多谢啊