如何使用OleDb + C#从csv文件读取数据时将RowNumber添加为列 [英] How to Add RowNumber as a column while reading data from a csv file, using OleDb + C#

查看:220
本文介绍了如何使用OleDb + C#从csv文件读取数据时将RowNumber添加为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#中的Oledb从csv文件读取数据
我还想用文件记录检索行号

我知道可以使用命令在Ole db中完成

I am reading data from a csv file using Oledb in C#
I also want to retrieve row number with the records of the file

I know this can be done in Ole db using the command

SELECT rank = ( SELECT COUNT(*)  FROM tableName b WHERE  a.ID > b.ID ), * FROM tableName a ORDER BY a.ID


这可以使用以下命令在SQl Server中完成:


this can be done in SQl Server using:

SELECT ROW_NUMBER() OVER (ORDER BY ID) AS Row  FROM TableName


但是我想从csv文件中读取数据,并且想使用上述任何查询在其中添加一列作为RowNumber

但这使我追随异常


But I want to read data from a csv file and there I want to add a column as RowNumber using any of the above query

But it is throwing me following exception

"No Value given for one or more parameters"


我的整个代码如下:


My whole code is as follows:

void ReadData(string FilePath)
{
  string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;", System.IO.Path.GetDirectoryName(FilePath));                   

  string CommandString = "SELECT rank = ( SELECT COUNT(*)  FROM {0} b WHERE  a.ID > b.ID ), * FROM {0}  a ORDER BY a.ID";

  CommandString = string.Format(CommandString ,Path.GetFileNameName(FilePath) );

  OleDbDataAdapter dataAdapter = new OleDbDataAdapter(CommandString , connString);
  DataTable dataSet = new DataTable();
  dataAdapter.Fill(dataSet); // this is giving "No Value given for one or more parameters" exception
  dgv.DataSource = dataSet;
}



任何人都可以提供任何帮助吗,我应该怎么做才能从csv文件中读取数据时再添加一列作为RowNumber



Can any one provide any help, what I should do to Add one more column as RowNumber while reading data from a csv file

推荐答案

命令字符串需要rank列以不同的方式被别名:
Command string needs rank column to be aliased in a different way:
SELECT ( SELECT COUNT(*) FROM {0} b WHERE  a.ID > b.ID ) AS rank, * FROM {0}  a ORDER BY a.ID


这篇关于如何使用OleDb + C#从csv文件读取数据时将RowNumber添加为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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