如何在.net做我的进口值从我想用OLEDB格式的CSV? [英] How in .Net do I Import Values from a CSV in the format I want using OleDB?

查看:164
本文介绍了如何在.net做我的进口值从我想用OLEDB格式的CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个包含看起来像整数字符串的列的CSV文件。这是他们应处理为字符串,但由于它们是数字,他们似乎被导入为整数(脱落的前导零)。

示例数据:

  • 0000000000079
  • 0000999000012
  • 0001002000005
  • 0004100000007

我看到的问题是,最后一个示例数据点来为通过的DBNull.Value。我假定这是因为OLEDB正在处理该列作为一个整数(数据点来通过未经其前导零也),并且0004100000007大于的最大整数值。

是否有某种方式说列[0]是一个字符串,不看它作为一个整数?当读取数据?

我目前使用的code是:

  OleDbConnection的dbConn =新的OleDbConnection(SourceConnectionString);
OleDbCommand的的DbCommand =新的OleDbCommand(SELECT * FROM test.csv,dbConn);
dbConn.Open();
OleDbDataReader dbReader = dbCommand.ExecuteReader();

而(dbReader.Read())
{
    如果(dbReader [0]!=的DBNull.Value)
    {
         //做一些工作
    }
}
 

解决方案

尝试在读者使用getString()方法时,你需要阅读列字符串:

 字符串myStringValue = reader.GetString(0);
 

I have a CSV file that has a column that contains strings that look like integers. That is they should be dealt with as strings, but since they are numbers they appear to be imported as integers (dropping off the leading zeroes).

Example Data:

  • 0000000000079
  • 0000999000012
  • 0001002000005
  • 0004100000007

The problem I'm seeing is that the last example data point comes through as DBNull.Value. I'm assuming this is because OleDB is treating that column as an integer (the data points come through without their leading zeroes also) and that 0004100000007 is greater than the largest integer value.

Is there some way to say "column [0] is a string, don't read it as an integer"? When reading the data?

The code I am currently using is:

OleDbConnection dbConn = new OleDbConnection(SourceConnectionString);
OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM test.csv", dbConn);
dbConn.Open();
OleDbDataReader dbReader = dbCommand.ExecuteReader();

while (dbReader.Read())
{
    if (dbReader[0] != DBNull.Value)
    {
         // do some work
    }
}

解决方案

Try to use GetString() method in the reader when you need to read the column as string:

string myStringValue = reader.GetString(0);

这篇关于如何在.net做我的进口值从我想用OLEDB格式的CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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