Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MVC和EF将CSV数据上载到SQL数据库_C#_Asp.net Mvc_Entity Framework_Csv - Fatal编程技术网

C# 使用MVC和EF将CSV数据上载到SQL数据库

C# 使用MVC和EF将CSV数据上载到SQL数据库,c#,asp.net-mvc,entity-framework,csv,C#,Asp.net Mvc,Entity Framework,Csv,我是一个新的MVC框架,试图弄明白如何解析CSV文件,以便只将某些列中的数据保存到数据库中 我可以选择CSV文件并通过视图上传,然后使用下面提到的代码将其传递给我的控制器 public ActionResult上传多文件(FileUploadViewModel fileModel) { //打开文件 if(Request.Files.Count==1) { //获取文件 var postedFile=Request.Files[0]; 如果(postedFile.ContentLength>0)

我是一个新的MVC框架,试图弄明白如何解析CSV文件,以便只将某些列中的数据保存到数据库中

我可以选择CSV文件并通过视图上传,然后使用下面提到的代码将其传递给我的控制器

public ActionResult上传多文件(FileUploadViewModel fileModel)
{
//打开文件
if(Request.Files.Count==1)
{
//获取文件
var postedFile=Request.Files[0];
如果(postedFile.ContentLength>0)
{
//从输入流读取数据
使用(var csvReader=new System.IO.StreamReader(postedFile.InputStream))
{
字符串inputLine=“”;
//读每一行
而((inputLine=csvReader.ReadLine())!=null)
{
//获取行值
字符串[]值=inputLine.Split(新字符[]{',});
对于(int x=0;x
然而,我并不确定如何在CSV文件中只选择所需的列并将其存储到数据库中


有什么建议吗?

通过创建一个
DataTable
方法解决了这个问题,方法是创建所需的列,然后使用
StreamReader
,并在行中循环并选择所需的列

[HttpPost]
public ActionResult UploadMultipleFiles()
{
    FileUploadService service = new FileUploadService();

    var postedFile = Request.Files[0];

    StreamReader sr = new StreamReader(postedFile.InputStream);
    StringBuilder sb = new StringBuilder();
    DataTable dt = CreateTable();
    DataRow dr;
    string s;
    int j = 0;

    while (!sr.EndOfStream)
    {
        while ((s = sr.ReadLine()) != null)
        {
            //Ignore first row as it consists of headers
            if (j > 0)
            {
                string[] str = s.Split(',');

                dr = dt.NewRow();
                dr["Postcode"] = str[0].ToString();
                dr["Latitude"] = str[2].ToString();
                dr["Longitude"] = str[3].ToString();
                dr["County"] = str[7].ToString();
                dr["District"] = str[8].ToString();
                dr["Ward"] = str[9].ToString();
                dr["CountryRegion"] = str[12].ToString();
                dt.Rows.Add(dr);
            }
            j++;
        }
    }
    service.SaveFilesDetails(dt);
    sr.Close();
    return View("Index");
}

您应该已经知道CSV文件的模式。通过获取
值[x]
可以根据特定列构建查询或分配给数据库变量
x
。这些CSV中平均有多少行?因为您正在访问一个文件,您应该考虑通过将同步操作方法转换为异步操作方法来将同步请求转换为异步请求。与答案无关,但您也可以查看此文件帮助器库来处理文件处理逻辑。
[HttpPost]
public ActionResult UploadMultipleFiles()
{
    FileUploadService service = new FileUploadService();

    var postedFile = Request.Files[0];

    StreamReader sr = new StreamReader(postedFile.InputStream);
    StringBuilder sb = new StringBuilder();
    DataTable dt = CreateTable();
    DataRow dr;
    string s;
    int j = 0;

    while (!sr.EndOfStream)
    {
        while ((s = sr.ReadLine()) != null)
        {
            //Ignore first row as it consists of headers
            if (j > 0)
            {
                string[] str = s.Split(',');

                dr = dt.NewRow();
                dr["Postcode"] = str[0].ToString();
                dr["Latitude"] = str[2].ToString();
                dr["Longitude"] = str[3].ToString();
                dr["County"] = str[7].ToString();
                dr["District"] = str[8].ToString();
                dr["Ward"] = str[9].ToString();
                dr["CountryRegion"] = str[12].ToString();
                dt.Rows.Add(dr);
            }
            j++;
        }
    }
    service.SaveFilesDetails(dt);
    sr.Close();
    return View("Index");
}