日期:2014-05-17  浏览次数:21475 次

C#对文件的写入一定要用.flush()方法才行么?
       private void button2_Click(object sender, EventArgs e)
        {
            if (ofdExport.ShowDialog() != DialogResult.OK)
                return;
            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_MyTable1";
                    using (FileStream fs1 = File.OpenWrite(ofdExport.FileName))
                    //using(FileStream fs = new FileStream("e:\\2.txt",FileMode.OpenOrCreate,FileAccess.Write))
                    {
                        StreamWriter sWrite = new StreamWriter(fs1);
                        //StreamWriter sWrite = new StreamWriter("e:\\2.txt", false, System.Text.Encoding.Default);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string name = reader.GetString(reader.GetOrdinal("Name"));
                                //MessageBox.Show(name);
                                int age = reader.GetInt32(reader.GetOrdinal("Age