gpt4 book ai didi

c# - 从 OleDbDataAdapter 填充数据集

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:49 25 4
gpt4 key购买 nike

我正在使用 OleDbDataAdapter 读取数据集中 Excel 工作表的内容。 Excel 工作表由 20391 行组成,数据集在我的本地机器上运行时读取总行数,但在 IIS7.5 服务器上运行代码时它只读取第一个 12463!!

我的连接字符串:

switch (strFileType.Trim())
{
case ".xls":
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath +
";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
break;
case ".xlsx":
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath +
";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
break;
}

var query = "SELECT * FROM [" + excelSheets[0] + "]";

//Create the connection object
var conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
var cmd = new OleDbCommand(query, conn);
var da = new OleDbDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);

有没有办法将da.Fill结果划分到多个数据表中?

最佳答案

我做了@dash 建议并使用了 Excel Data Reader并且它工作正常。

这是代码

        FileStream stream = File.Open(strNewPath , FileMode.Open, FileAccess.Read);            
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);;
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

关于c# - 从 OleDbDataAdapter 填充数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15735184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com