首页 新闻 会员 周边 捐助

C# io 操作问题请教,FileStream StreamReader StreamWriter这些类如何去使用。

0
悬赏园豆:10 [已解决问题] 解决于 2012-01-05 13:31

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))
也行,
小弟深感迷惑,请高手指点迷津,感激不尽

代号极光的主页 代号极光 | 初学一级 | 园豆:146
提问于:2012-01-02 14:48
< >
分享
最佳答案
1

FileStream包括读写操作; StreamReader 只是读操作;StreamWriter只是写操作;

收获园豆:10
画方软件 | 小虾三级 |园豆:778 | 2012-01-04 15:07

多谢啊

代号极光 | 园豆:146 (初学一级) | 2012-01-05 13:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册