ADO.NET 학습 노트(9) 연습: 데이터 내보내기

2901 단어 .net
private void btnExport_Click(object sender, EventArgs e)

{

  string filePath = @"F:\WXX\     1\      \  .txt";

  if (!File.Exists(filePath))

  {

    using (FileStream fs = File.Create(filePath)) { }

  }



  using (FileStream fileStream = File.OpenWrite(filePath))

  {

    using (StreamWriter streamWriter = new StreamWriter(fileStream))

    {

      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_Province";

          using (SqlDataReader reader = cmd.ExecuteReader())

          {

            while (reader.Read())

            {

              streamWriter.WriteLine(reader.GetString(reader.GetOrdinal("FProvince")) + "|" + reader.GetString(reader.GetOrdinal("FCity")));

            }

          }

        }

      }

    }

  }

  MessageBox.Show("    ");

}

좋은 웹페이지 즐겨찾기