使用Visual Studio C#导入CSV文件 [英] Import CSV file with Visual studio C #

查看:444
本文介绍了使用Visual Studio C#导入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个使用C#语言的网站项目。我的想法是使用Visual Studio的FileUpload工具将文件导入我的应用程序。我需要的是导入一个包含数十个数据的CSV文件,以便将这些数据插入我的数据库。

一个例子是我有一个表Customer,它具有以下结构:

id_custumer int IsIdentity,

名称varchar(100),

姓氏varchar(100),

age int,

locality varchar(200)



正如我所说,我创建了一个包含各种客户数据的csv文件。我想通过FileUpload将这个文件导入我的应用程序并将数据添加到我的表customer。



任何想法怎么做?

hello,
 
I have a website project with C # language. my idea is to use the FileUpload tool of Visual Studio to import files to my application. what i need is to import a CSV file that contains dozens of data, in order to insert these data in my database.
an example is I have a table "Customer" which has the following structure:
id_custumer int IsIdentity,
name varchar (100),
surname varchar (100),
age int,
locality varchar (200)

as I said, I have created a csv file that contains various customer data. I want, through FileUpload, import this file into my application and adding the data to my table "customer".

any idea how to do this?

推荐答案

阅读CSV文件是一个非常基本的概念,如果你使用谷歌,有成千上万的链接如何做。



一旦你有了一个可以读取CSV的工具,下一个阶段就是在你的数据库中插入数据(我假设它是一个SQL数据库,但如果不是那么它应该仍然是公平的直截了当)。你需要谷歌SQL插入语句和SQL命令对象(.Net)。



一旦你有这两个工作,那么你需要的只是一个文件上传机制您的网站将文件发送到服务器并在文件传输后触发导入。再次通过谷歌很容易找到,我应该知道我必须在6周前查看同样的事情因为我完全忘记了怎么做。



你这里没有做任何努力,也没有做过1000次之前没有做过的事情,所以试着去搜索东西,而不是期待代码交给你。



我建议您查找CSV帮助程序作为帮助阅读CSV文件的工具,并确保将一些验证编码到流程中,以防人们通过错误格式化的文件。

同时查找SQLParameters通过文件上传过程帮助防止SQL注入攻击。





一旦基本流程到位,我建议你考虑一下限制,如果您要上传1000条记录,您可能需要考虑对文件进行异步处理,以便您可以通知用户进度,或者只是提交文件并在后台运行导入线程并在完成/失败后发送电子邮件。
Reading a CSV file is a very basic concept and there are thousands of links on how to do it if you use google.

Once you have a tool that can read the CSV the next stage is to insert the data in the your database (I am making an assumption that it is an SQL database, but if it is not then it should still be fairly straight forward). You need to google SQL Insert Statement and the SQL Command object (.Net).

Once you have those two working, then all you need is a file upload mechanism in your website that sends the file to the server and triggers the import once the file is transferred. Again this is easy to find via Google, I should know I had to look up the same thing about 6 weeks ago as I completely forgot how to do it.

You are not doing anything hard here, nor anything that has not been done 1000's of times before, so try actually searching for stuff rather than expecting the code to be handed to you.

I suggest you look up CSV helper as a tool to help with reading the CSV file, and make sure you encode some validation into the process incase people pass you incorrectly formatted files.
Also look up SQLParameters to help prevent against SQL Injection attacks via the file upload process.


Once you basic processes are in place, I suggest you consider the limitations, if you are uploading 1000's of records you might want to consider asynchronous processing of the file so you can inform the user of progress, or just have the file submitted and run the import on a background thread and send out an e-mail once it is complete/failed.


你好吗,



如果这是你的话期待请通过这个链接。



用C#读取和写入CSV文件 [ ^ ]

快速CSV阅读器 [ ^ ]

http://stackoverflow.com/questions/1050112/how-to-read-a-csv-file-into-a-net-datatable [ ^ ]
Hi fasher,

If this is what you are expecting please go through this links.

Reading and Writing CSV Files in C#[^]
A Fast CSV Reader[^]
http://stackoverflow.com/questions/1050112/how-to-read-a-csv-file-into-a-net-datatable[^]


请检查 here 示例


这篇关于使用Visual Studio C#导入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆