如何在asp.net中导入CSV文件? [英] how import CSV file in asp.net?
本文介绍了如何在asp.net中导入CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友您好,
我正在尝试导入CSV文件,但我遇到了很多问题。
比如,如何读取csv文件然后如何在数据库中插入值?
任何人都可以指导我吗?
问候
迪帕克
Hello friends,
I am trying to import CSV file but I have many problems.
Like, how to read csv file and then how to insert the value in the database?
Can any one guide me ?
Regards
Deepak
推荐答案
试试这个链接
导入CSV文件 [ ^ ]
有很多方法可以实现这一目标。
一种方法可以是
Step1:尝试将所有数据读入数据表(假设第一行CSV表示ColumnName)
There are many ways to achieve this.
One ways can be
Step1:Try to read all your data in to a datatable ( assuming 1st line of CSV represents the ColumnName)
var values = GetAllLines();
DataTable dt = new DataTable();
foreach (var item in values.FirstOrDefault())
{
if (!dt.Columns.Contains(item))
dt.Columns.Add(item);
}
var allvalues=values.Skip(1).ToList();
for (int i = 0; i < allvalues.Count; i++)
{
dt.Rows.Add(allvalues.Take(dt.Columns.Count).ToArray());
}
Get All Lines函数的描述
Description for Get All Lines function
private IEnumerable<string[]> GetAllLines()
{
string str;
using (StreamReader rd=new StreamReader(txtFileName.Text))
{
while ((str = rd.ReadLine()) != null)
{
yield return str.Split(',');
}
}
}
STEP2 然后,您可以使用SQL批量复制将整个数据表复制到数据库。在需要设置目标表和列映射的地方,在我的情况下,我假设列名与datatable的列名相同
STEP2Then you can use SQL Bulk Copy to Copy the Entire datatable to database. Where you need to set the destination table and Column Mapping, in my case i assume the column name is same as that of datatable
SqlConnection dbconnection = new SqlConnection("your Connection string.");
SqlBulkCopy sqlblkcpy=new SqlBulkCopy(dbconnection);
dbconnection.Open();
sqlblkcpy.DestinationTableName = "<Table Name To Which you want to Copy your Data>";
foreach (DataColumn item in dt.Columns)
{
sqlblkcpy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(item.ColumnName, item.ColumnName));
}
if(dt.RecordCount>0)
sqlblkcpy.WriteToServer(dt);
试试这段代码。
请正确开始
Try to this code.
Please start properly
STEP1: IN THE PAGE ( sample.aspx)
INSERT THE FOLLOWING CODE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sample.aspx.cs" Inherits="sample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Select File:
<asp:FileUpload ID="FileUploader" runat="server" />
<br />
<br />
<asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
STEP2:
in the code page say for example ( sample.aspx.cs)
INSERT THE FOLLOWING CODE:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class sample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploader.HasFile)
try
{
FileUploader.SaveAs(Server.MapPath("confirm//") +
FileUploader.FileName);
Label1.Text = "File name: " +
FileUploader.PostedFile.FileName + "<br>" +
FileUploader.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUploader.PostedFile.ContentType + "<br><b>Uploaded Successfully";
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
}
}
这篇关于如何在asp.net中导入CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文