Asked 7 years, 8 months ago. Active 3 years, 1 month ago. Viewed 6k times. Append "" ; if sline! Improve this question. Aman Aman 2, 2 2 gold badges 15 15 silver badges 18 18 bronze badges. Have you considered using Microsoft. It removes all the complexities associated with CSV files. So the , symbol is the separator for each column. Are the imbedded commas somehow marked as being text?
If not then even a 3rd person csv parser will run into problems there. But if they are you can either use a 3rd person csv-parser OR adapt your code with the help of regex so that those , don't count. Add a comment. Active Oldest Votes. Add "Name" , "Name" ; objbulk. Add "City" , "City" ; objbulk. Add "Address" , "Address" ; objbulk. Open ; objbulk. WriteToServer csvdt ; con.
Add "Name" ; tblcsv. Add "City" ; tblcsv. BulkCopy "TableName", dataReader ; There are also additional configuration options for more complex scenarios flexible column mapping, additional static column values which are not present in the csv file, value transformation.
ColumnName, column. Michal Ciesielski Michal Ciesielski 3 3 silver badges 8 8 bronze badges. WriteLine e. WriteToServer p. Cinchoo Cinchoo 5, 2 2 gold badges 16 16 silver badges 32 32 bronze badges.
I have two steps at runtime load CSV with new headers dynamically to DB, create a new table with all the headers from the CSV this code above is helping with this, but in the same call I want to get back an IList from the table also, so in step 2 how can I get newTableName.
Then use it to bind to Grid also write to table. Extended documentation can be found on code project: codeproject. Tip: in the CSV file, let the first column be blank with a header for your auto increment the database column.
Community Bot 1 1 1 silver badge. The trick is that it implements a IDataReader class to read the. ToString End Function Be carfully here though since tablename and csv name has to be identical as well as the column count in the csv has to be the same as in the pre-defined table. SetBasePath Path. GetDirectoryName System.
AddJsonFile "appsettings. GetSection "ConnectionString". Securitiesinfo"; foreach var column in dt. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. All it does is read the csv and parrot out its contents on the Console:. For a resilient, fast, and low effort solution, you can use CsvHelper which handles a lot of code and edge cases and has pretty good documentation.
Creating array to keep the information is not a very good idea, as you don't know how many lines will be in the input file. What would be the initial size of your Array?? I would advise you to use for example a Generic List to keep the information E. You can also add a constructor to your Sport Class that accepts an array result of the split action as described in above answer.
Just for simplicity I wrote the Convert Method, but keep in mind this is also not a very safe way unless you are sure that the DateField always contains valid Dates and Score always contains Numeric Values. You can try other safer methods like tryParse or some Exception Handling. I all honesty, it must add that the above solution is simple as requested , on a conceptual level I would advise against it. Putting the mapping logic between attributes and the csv-file in the class will make the sports-class too dependent on the file itself and thus less reusable.
Any later changes in the file structure should then be reflected in your class and can often be overlooked. While there are a lot of libraries that will make csv reading easy see: here , all you need to do right now that you have the line, is to split it.
This will however overwrite the values each time you read a new line, so you need to pack the values into a class and save the instances of that class to a list. Linq also has a solution for this and you can define your output as either a List or an Array. In the example below there is a class that as the definition of the data and data types.
Below is for newbie and eye catching solution that most newbie like to try and error please don;t forget to add System. Maybe like this, the sample above is creating your own iteration using yield please look at MSDN documentation for that and create collection based on your string.
Let me know if I write the code wrong since I don;t have Visual studio when I write the answer. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
0コメント