Importing CSV files using OleDB - string datatype?

99封情书 提交于 2019-12-11 07:27:46

问题


I'm writing a C# .net web application, and users will be uploading CSV files to be processed. These CSV files contain a number of data types, and I'm having trouble with a few of them. There is a 20-digit receipt number field that is getting imported in scientific notation (9.1234563E+18 instead of 91234563000000000000), and any leading zeros are getting dropped. Leading zeros are also dropped in another 4-digit store number field.

I've read about creating a Schema.ini file to define the datatypes, but each file will have a different file name, meaning I'd have to generate a new Schema file programmatically, and since multiple users may be using the application simultaneously, it kind of rules this option out.

Does anybody have any advice?


回答1:


Is this CSV file created in Excel? if it is, ensure the users have the columns set to the right data types. I had a similar issue before, but it was due to the users have a date field set as a different type - so instead of getting back something like 20100408, i would get like 24782 :-)

Otherwise, try reading in each field has straight text....and then handling the conversions yourself




回答2:


It sounds like you will have to write some custom code that reads the files manually and split the fields yourself. You can easily read in a line at a time and use the String.Split method to get the individual fields. Of course this is assuming the CSV files will always be in the same format so you can process each field the same way no matter where the file comes from. This way you can process each field exactly the way you want to.



来源:https://stackoverflow.com/questions/5747776/importing-csv-files-using-oledb-string-datatype

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!