推断从输入流读取的数据类型 [英] inferring the type of data read from an inputstream

查看:86
本文介绍了推断从输入流读取的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望实现以下目标;我有一个CSV文件,我想将其读入DataTable对象.我对文件唯一了解的是第一行包含标头名称,各列的类型分别为doubleintstring(或者实际上是enum具有可能的值,该列中的唯一字符串集).问题是,如何创建每个列都具有正确类型的数据表?

基本上我的设置如下:

Hi,

I am looking to achieve the following; I have a CSV file that I want to read into a DataTable object. The only thing I know about the file is that the first row contains header names and that the various columns are either of the type double, of the type int or of the type string (or actually an element of an enumwith possible values the set of unique strings in the column). The question is, how to create a datatable in which each column has the right type?

Basically my set-up looks like:

private char[] inputDelimiter = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToCharArray();
private DataTable output = new DataTable();

using( StreamReader myReader = new StreamReader(fileName) )
            {
                string[] varNames = myReader.ReadLine().Split(inputDelimiter);
                for (int colCntr = 0; colCntr < varNames.Length; colCntr++)
                {
                     output.Columns.Add(new DataColumn(varNames[colCntr]));
                }

                // code needed here to determine the data type of each column;

                string nextLine;
                while ( (nextLine = myReader.ReadLine()) != null )
                {
                    // I''ll fill the datatable here myself
                }


            }




任何想法都将不胜感激.




Any thoughts are greatly appreciated.

推荐答案

使用int.TryParse,然后使用double.TryParse来计算数据是double还是int,然后再计算,您可以假设它只是一个字符串.
Use int.TryParse, then double.TryParse to work out if the data is a double, or an int, then after that, you can assume it''s just a string.


这篇关于推断从输入流读取的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆