OledbDataAdapter.Fill(DataTable)方法出错,但是不报错!!!

liubaoen 2011-11-22 05:48:39
问题描述:
没有任何输出,直接退出!!!!关键是为什么会直接退出不报错呢??????
程序不报任何错!!!
已经尝试的方法:
基本确定问题应该在oda.Fill(dt) 这个方法上,但是使用try不能捕获错误.
跟踪调试,发现程序执行oda.Fill(dt); 语句是直接退出,根本没有将dataTable返回.

using System;
using System.Data;

namespace ScoreReportSystem
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");

// TODO: Implement Functionality Here
test();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}

private static void test()
{
ExcelReader er=new ExcelReader("D:\\score2009");
System.Data.DataTable dt=er.getFirstSheetData();
PrintTable(dt);
}

private static void PrintTable(DataTable table,int top=0)
{
if(top==0)
{
top=table.Rows.Count;
}

int count=table.Columns.Count;
for(int row=0;row<top;row++)
{
for(int i=0;i<count;i++)
{
Console.Write(table.Rows[row][i].ToString()+"\t");
}
Console.WriteLine();
}
}



}
}






using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;

namespace ScoreReportSystem
{
/// <summary>
/// Description of MyClass.
/// </summary>
public class ExcelReader
{
private string _fileName;
public ExcelReader(string fileName)
{
_fileName=fileName;
}
public DataTable getFirstSheetData()
{
string cnstr=string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1';",_fileName);
string selectCommandText="select top 5 * from [$A1:R65536]";
OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,cnstr);
DataTable dt=new DataTable();
oda.Fill(dt); //出错的地方
return dt;
}
}
}

...全文
473 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
oLuoLuoWuChen1 2013-12-12
  • 打赏
  • 举报
回复
我也遇到这个问题了,请问最后找到原因了吗?
ataisite198906 2013-02-27
  • 打赏
  • 举报
回复
哥们儿,那你这个最后找到原因了吗
liubaoen 2011-11-24
  • 打赏
  • 举报
回复
这个问题,感觉有点抽风的感觉,如果我新建一个工程,然后将代码重写一次(复制一次也可以),结果可以运行,而且运行的很好,实在找不到问题的核心.
因为赶工期,只能把代码复制到一个新的工程中,目前运行没有问题了.....
fantisGod 2011-11-22
  • 打赏
  • 举报
回复
如果是fill问题的话,改成这样oda.Fill(dt,"[$A1:R65536]");
阿非 2011-11-22
  • 打赏
  • 举报
回复
OleDbConnection connetion = new OleDbConnection(cnstr);
OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,connetion);
---------------------
断开式 链接 不用显式打开

liubaoen 2011-11-22
  • 打赏
  • 举报
回复
感谢楼上的回复,你这个不是问题,因为有这个重载方法:OleDbDataAdapter(string selectCommandText,string selectConnectionString),
如果是这个问题,系统不会不报错的!
还是很感谢您的回复.
DENQH 2011-11-22
  • 打赏
  • 举报
回复

OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,cnstr);
cnstr是string 当然错了

OleDbConnection connetion = new OleDbConnection(cnstr);
connetion.Open();
OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,connetion);

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧