C# 如何将txt或csv文件上载到windows form c

C# 如何将txt或csv文件上载到windows form c,c#,file-upload,C#,File Upload,我想通过c语言的windows窗体上传一个txt或csv文件,并想将数据导入数据库sql server。我该怎么做?我面临两个问题:- 我没有任何上传文件的工具,只有图像上传工具可用 若我设法完成了文件上传,那个么如何将数据从txt文件导入sql server数据库 如果有人能通过c代码解决方案提供帮助,那将是一件好事,对我很有帮助。您可以尝试以下方法: private void buttonGetFile_Click(object sender, EventArgs e) { Open

我想通过c语言的windows窗体上传一个txt或csv文件,并想将数据导入数据库sql server。我该怎么做?我面临两个问题:-

我没有任何上传文件的工具,只有图像上传工具可用

若我设法完成了文件上传,那个么如何将数据从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
        }
    }
}

您应该提供更多的内容,比如:您使用的是什么系统?桌面、网站,。。?到目前为止你尝试了什么?等将csv加载到RDBMS是典型的导入任务,有很多方法可以执行。不要重新发明轮子。