簡體   English   中英

如何將txt或csv文件上傳到windows表單c#

[英]how to Upload a txt or csv file to windows form c#

我想通過 c# 中的 windows 表單上傳 txt 或 csv 文件,並希望將數據導入數據庫(sql server)。 我怎樣才能做到這一點? 我面臨兩個問題:-

  1. 我沒有任何文件上傳工具,只有圖片上傳工具可用。

  2. 如果我以某種方式設法進行文件上傳,那么我如何將數據從 txt 文件導入 sql server 數據庫?

如果有人可以通過 c# 代碼解決方案提供幫助,這對我來說會很好而且很有幫助嗎?

您可以嘗試以下操作:

private void buttonGetFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "Text files | *.txt"; // file types, that will be allowed to upload
    dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
    if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
    {
        String path = dialog.FileName; // get name of file
        using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open), new UTF8Encoding())) // do anything you want, e.g. read it
        {
            // reader will be having all data
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM