【问题标题】:how to Upload a txt or csv file to windows form c#如何将 txt 或 csv 文件上传到 windows 窗体 c#
【发布时间】:2015-12-03 16:48:45
【问题描述】:

我想在 c# 中通过 windows 窗体上传 txt 或 csv 文件,并希望将数据导入数据库(sql server)。我怎样才能做到这一点?我面临两个问题:-

  1. 我没有任何文件上传工具,只有图片上传工具可用。

  2. 如果我设法上传文件,我该如何将数据从 txt 文件导入 sql server 数据库?

如果有人可以通过 c# 代码解决方案提供帮助,那将对我有帮助吗?

【问题讨论】:

  • 您应该提供更多内容,例如:您使用的是什么系统?桌面,网站,..?到目前为止你尝试了什么?等等。stackoverflow.com/help/how-to-ask
  • csv 加载到RDBMS 是* 典型的导入任务,执行它的方法有很多种。不要重新发明轮子。

标签: c# file-upload


【解决方案1】:

您可以尝试以下方法:

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
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 2021-11-27
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2016-03-04
    • 2020-02-17
    相关资源
    最近更新 更多