我想使用实体框架将数据从CSV文件导入SQL数据库 [英] I want to import data from CSV file to SQL database using Entity Framework
本文介绍了我想使用实体框架将数据从CSV文件导入SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数据从CSV文件插入到SQL表中.这是我的代码,我不知道该怎么做
I want insert data from CSV file to SQL table. Here is my code I don't know how to go further
var readcsv = File.ReadAllText(filepath);
string[] csvfilerecord = readcsv.Split('\n');
foreach (var row in csvfilerecord)
{
if (!string.IsNullOrEmpty(row))
{
foreach (var cell in row.Split(','))
{
var cards = new Cards
{
//What to do here to assign data to each column
};
}
}
}
这是我的csv文件数据
here is my csv file data
12345,cvv,18-april,name,country,address,state,city,dob,zipcode,phone,email,10
12345,cvv,18-april,name,country,address,state,city,dob,zipcode,phone,email,10
类定义
public string nummber { get; set; }
public string cvv { get; set; }
public string expDate { get; set; }
public string name { get; set; }
public string country { get; set; }
public string address { get; set; }
public string state { get; set; }
public string dob { get; set; }
推荐答案
var readcsv = File.ReadAllText(filepath);
string[] csvfilerecord = readcsv.Split('\n');
foreach (var row in csvfilerecord)
{
if (!string.IsNullOrEmpty(row))
{
var cells = row.Split(','))
var card = new Cards
{
number = cells[0], // number is in first cell
cvv = cells[1], // cvv is in second cell
// ...
};
}
}
这篇关于我想使用实体框架将数据从CSV文件导入SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文