c# - Writing CSV files into Sql database using LinqToCsv - .NET MVC 4 - Entity Framework -


i trying create application in user can upload .csv files sql database have created. have become little confused when comes getting file path view , writing database.

first off, here model i'm working off:

    public class outstandingcreditcsv {     [csvcolumn(fieldindex = 1, canbenull = false)]     public string ponumber { get; set; }     [csvcolumn(fieldindex = 2, outputformat = "dd mmm hh:mm:ss")]     public datetime creditinvoicedate { get; set; }     [csvcolumn(fieldindex = 3)]     public string creditinvoicenumber { get; set; }     [csvcolumn(fieldindex = 4, canbenull = false, outputformat = "c")]     public decimal creditinvoiceamount { get; set; } } 

and here controller code far:

        public actionresult index()     {         csvfiledescription inputfiledescription = new csvfiledescription         {             separatorchar = ',',             firstlinehascolumnnames = true         };          var context = new csvcontext();          ienumerable<outstandingcreditcsv> csvlist =         context.read<outstandingcreditcsv>("c:/users/blahblah/desktop/csvuploadtestfile.csv", inputfiledescription);          foreach (outstandingcreditcsv line in csvlist)         {          }          return view();     } 

there 2 areas need little guidance. i'm not sure how pass file view controller, lets view simple this:

<form action="" method="post" enctype="multipart/form-data"> <table style="margin-top: 150px;">     <tr>         <td>             <label for="file">filename:</label>         </td>         <td>             <input type="file" name="file" id="file"/>         </td>         <td><input type="submit" value="upload"/></td>     </tr> </table> </form> 

i'm unsure how loop csv data database. can see foreach loop in controller empty. appreciated. thanks!

i editing post answer part of question missed. doing here uploading csv file temporary location, reading object if want can delete csv file out of temporary location (not shown).

[httppost] public jsonresult uploadvalidationtable(httppostedfilebase csvfile) {     csvfiledescription inputfiledescription = new csvfiledescription     {          separatorchar = ',',          firstlinehascolumnnames = true     };      var cc = new csvcontext();     string filepath = uploadfile(csvfile.inputstream);     var model = cc.read<outstandingcreditcsv>(filepath, inputfiledescription);      //if csv has several rows convert list of model     //var model = cc.read<list<outstandingcreditcsv>>(filepath, inputfiledescription);    //then can loop through , same below    /*foreach(var row in model)    {         var invoice = row.creditinvoicenumber;    }*/      try     {         //do need here, save items database         var invoice = model.creditinvoicenumber;          var invoicetable = yourcontext.yourtable             .firstordefault(x => x.yourtableid == passedinid);         invoicetable.creditinvoicenumber = model.creditinvoicenumber;        yourcontext.savechanges();     }     catch(linqtocsvexception ex)     {      }      return json(model, "text/json"); }  private string uploadfile(stream serverfilestream) {     string directory = "~/content/csvuploads";      bool directoryexists = system.io.directory.exists(server.mappath(directory));      if (!directoryexists)     {         system.io.directory.createdirectory(server.mappath(directory));     }      string targetfolder = server.mappath(directory);     string filename = path.combine(targetfolder, guid.newguid().tostring() + ".csv");      try     {         int length = 256; //todo: replace actual length         int bytesread = 0;         byte[] buffer = new byte[length];          // write required bytes         using (filestream fs = new filestream(filename, filemode.create))         {                         {                 bytesread = serverfilestream.read(buffer, 0, length);                 fs.write(buffer, 0, bytesread);             }             while (bytesread == length);         }          serverfilestream.dispose();         return filename;     }     catch (exception ex)     {         return string.empty;     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -