没有给出一个或多个必需参数的值 - 从OLEDB采用者对象填充数据表时出现异常 [英] No value given for one or more required parameters - Exception when filling data table from OLEDB adopter object

查看:69
本文介绍了没有给出一个或多个必需参数的值 - 从OLEDB采用者对象填充数据表时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的程序是将.CSV文件的内容读取到数据表。

我使用过OLEDB数据采用者阅读CSV文件。下面是代码。

Hi All,

My program is to read the contents of .CSV file to data table.
I have used OLEDB data adopter to read the CSV file. Below is the code.

public DataTable readCSV(String FileName)
   {
       OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + FileName + ";Extended Properties=text;HDR=Yes;FMT=Delimited");
       string query = "select F1 as [First Column Name], F2 as [Second Column name] from [" + FileName + "]";
       con.Open();
       OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, con);
       DataTable dt = new DataTable();
       DataAdapter.Fill(dt);
       return dt;

   }





当CSV文件至少有一行时工作正常。但是当文件是BLANK(没有要读取的行)时,我会在DataAdapter.Fill(dt)中获得异常 - 没有给出一个或多个必需参数的值。



可以请一些人查看我的代码并帮我跟踪.CSV文件为BLANK时出现的确切问题。



提前致谢。

Harsha。



It is working fine when the CSV file have at least one row. But When the file is BLANK(No rows to read), I am getting exceptions as - "No value given for one or more required parameters" at DataAdapter.Fill(dt).

Can some one please review my code and help me to trace what exactly is going wrong when the .CSV file is BLANK.

Thanks in advance.
Harsha.

推荐答案

不要使用OleDb来读取CSV。认真。它是.Net框架中微软的继子女之一。它有各种各样的怪癖。使用它,这太棒了(看看那篇文章的评级和投票数量):



快速CSV阅读器 [ ^ ]



编辑:



当然,当您尝试读取异常的CSV文件时,CSV-Reader也会抛出错误。我不知道它是否默认将空行视为错误 - 但您可以配置它应该如何处理某些情况。如果您在将其纳入项目后仍然困难,请随时再次询问。 (或者看看文章末尾的评论,可能是其他人已经问过同样的问题了。)
Don't use OleDb for reading CSV. Seriously. It's one of Microsofts stepchildren in the .Net framework. It has all kinds of quirks. Use this, it's fantastic (look at the rating and amount of votes on that article):

A Fast CSV Reader[^]

edit:

Of course that CSV-Reader can also throw errors when you try to read "unusual" CSV-files. I don't know if it treats blank lines as errors by default or not - but you can configure how it should handle certain situations. If you're still stuck after incorporating it into your project, feel free to ask again. (Or look at the comments at the end of the article, probably someone else already asked the same question.)


这篇关于没有给出一个或多个必需参数的值 - 从OLEDB采用者对象填充数据表时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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