读取 Excel 文件 - 没有为一个或多个必需参数指定值 [英] Reading Excel file - No value given for one or more required parameters

查看:94
本文介绍了读取 Excel 文件 - 没有为一个或多个必需参数指定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

更新正确的路径后,我收到新的错误消息外部表不是预期的格式."

我不确定这段代码有什么问题,我正在尝试阅读 excel 表,下面是我的代码并收到以下错误.

OleDbConnection oledbConn = new OleDbConnection();字符串路径 = Path.GetFullPath(Server.MapPath("~/InformationNew.xlsx"));oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");oledbConn.Open();OleDbCommand cmd = 新 OleDbCommand();OleDbDataAdapter oleda = new OleDbDataAdapter();数据集 ds = 新数据集();cmd.Connection = oledbConn;cmd.CommandType = CommandType.Text;cmd.CommandText = "SELECT [epic],[desc] FROM [Sheet1$]";oleda = 新的 OleDbDataAdapter(cmd);oleda.Fill(ds);//<<<错误

<块引用>

System.Data.OleDb.OleDbException"类型的异常发生在System.Data.dll 但未在用户代码中处理

附加信息:没有为一个或多个必需参数给出值.

解决方案

我以前见过这个.确保您的电子表格名称正确(如果偏离一个字符则无效)、列名称正确且路径有效.

将您的数据访问封装在 Try...catch...finally 块中,您可能会获得更多错误数据.

UPDATE:

after updating the correct path, I'm getting the new error which says "External table is not in the expected format."

I'm not sure what is wrong with this code and I'm trying to read the excel sheet and below is my code and getting the following error.

OleDbConnection oledbConn = new OleDbConnection();
string path =  Path.GetFullPath(Server.MapPath("~/InformationNew.xlsx"));

oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");

oledbConn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
cmd.Connection = oledbConn;
cmd.CommandType = CommandType.Text;

cmd.CommandText = "SELECT [epic],[desc] FROM [Sheet1$]";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds); //<<<ERROR

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code

Additional information: No value given for one or more required parameters.

解决方案

I've seen this before. Make sure your spreadsheet name is correct (if you're off by one character it won't work), your column names are correct, and your path is valid.

Wrap your data access in a Try...catch...finally block and you can probably get more error data.

这篇关于读取 Excel 文件 - 没有为一个或多个必需参数指定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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