自學.net(2)資料的匯入和匯出

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   ar   for   檔案   資料   

 1 using Microsoft.Win32; 2 using System; 3 using System.Collections.Generic; 4 using System.Data.SqlClient; 5 using System.IO; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows;10 using System.Windows.Controls;11 using System.Windows.Data;12 using System.Windows.Documents;13 using System.Windows.Input;14 using System.Windows.Media;15 using System.Windows.Media.Imaging;16 using System.Windows.Navigation;17 using System.Windows.Shapes;18 19 namespace wenjianduqu20 {21     /// <summary>22     /// MainWindow.xaml 的互動邏輯23     /// </summary>24     public partial class MainWindow : Window25     {26         public MainWindow()27         {28             InitializeComponent();29         }30 31         private void btnImport_Click(object sender, RoutedEventArgs e)32         {33             OpenFileDialog ofg = new OpenFileDialog();34             ofg.Filter = "文字檔|*.txt";
         //如果沒選擇任何檔案,則返回
35 if (ofg.ShowDialog()!=true)36 {37 return;38 }39 string filename = ofg.FileName;
          //返回的是一個數組,所以要用IEnumerable介面
40 IEnumerable<string> lines = File.ReadLines(filename,Encoding.Default);
            //需要考慮資料讀取和寫入的編碼問題,所以要用Encoding.Default,要不然會出亂碼
41 foreach (string line in lines)42 {43 string[]segs=line.Split(‘|‘);44 string name=segs[0];45 string age=segs[1];46 SqlHelper.ExecuteNonQuery("insert into T_Customer (Name,Age)
              values(@Name,@Age)
",47 new SqlParameter("@Name", name), new SqlParameter("@Age",
              Convert.ToInt32(age)));
48 }49 MessageBox.Show("匯入成功!成功匯入"+lines.Count()+"條資料");50 } 51 }52 }

 

自學.net(2)資料的匯入和匯出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.