如何使用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 needsrank
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