简体   繁体   English

EXCEL 的 C# OLEDB 连接在循环中不起作用

[英]C# OLEDB connection for EXCEL not working on loop

1-I am having a strange problem. 1-我有一个奇怪的问题。

Requirement: read a list of excel files on a folder and store the content on a dataset.要求:读取文件夹中的 excel 文件列表,并将内容存储在数据集上。

What I have written: Button click:我写的:按钮点击:

protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
{
            string[] strFiles = Directory.GetFiles(strPath);
            foreach (string strFile in strFiles)
            {
                ClsExcelUpload objExcelUpload = new ClsExcelUpload();
                string szConnectionString1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFile + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
                objExcelUpload.ReadExcel(szConnectionString1, strFile);

            }
}

function ReadExcel: function 读取Excel:

public string ReadExcel(string szConnectionString, string strFile)
    {
        OleDbConnection objConn = new OleDbConnection();
        objConn.ConnectionString = szConnectionString;
        objConn.Open();

        OleDbDataAdapter DAobjBasicInfo = new OleDbDataAdapter();
        DAobjBasicInfo = new OleDbDataAdapter("select * from [Sheet1$]", objConn);
        DataTable dt1 = new DataTable();
        DAobjBasicInfo.Fill(dt1);
        objConn.Close();
        objConn.Dispose();
        return "";
    }

Problem: The szConnectionString gets updated with the new filename set through the loop but when the OleDbDataAdapter opens the objConn, it looks for the Excel file which is already deleted.问题: szConnectionString 使用通过循环设置的新文件名进行更新,但是当 OleDbDataAdapter 打开 objConn 时,它会查找已删除的 Excel 文件。 and the datatable everytime gets the first file data everytime.并且数据表每次都获取第一个文件数据。

This is the strangest thing I have ever seen as I am not able to identify the area of problem why the oledbconnection is not getting updated.这是我见过的最奇怪的事情,因为我无法确定 oledbconnection 没有得到更新的问题区域。


entire code:整个代码:

using System.Data;使用 System.Data;

using System.Data.OleDb;使用 System.Data.OleDb;

    protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
    {
        HttpFileCollection files = Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            if (file.ContentLength > 0)
            {
                FileUploadExcel.SaveAs(Server.MapPath(@"~\UploadedFile\") + file.FileName);

            }
        }
    string strPath = Path.GetFullPath(Server.MapPath(@"~\UploadedFile\"));
    string[] strFiles = Directory.GetFiles(strPath);
    foreach (string strFile in strFiles)
    {
        ClsExcelUpload objExcelUpload = new ClsExcelUpload();
        string szConnectionString1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFile + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
      string result=  ReadExcel(szConnectionString1, strFile);

    }
}


    public string ReadExcel(string szConnectionString, string strFile)
    {
        OleDbConnection objConn = new OleDbConnection();
        objConn.ConnectionString = szConnectionString;
        objConn.Open();

        OleDbDataAdapter DAobjBasicInfo = new OleDbDataAdapter();
        DAobjBasicInfo = new OleDbDataAdapter("select * from [Sheet1$]", objConn);
        DataTable dt1 = new DataTable();
        DAobjBasicInfo.Fill(dt1);
        objConn.Close();
        objConn.Dispose();
        File.Delete(strFile);
        return "";
    }

Further analysis on this strange issue: I would try to explain in more detail.关于这个奇怪问题的进一步分析:我会尝试更详细地解释。 I have one FileUpload control which allows multiple files to select and upload.我有一个 FileUpload 控件,它允许多个文件到 select 并上传。 a command button which copies the posted file through FileUpload to a specified location.一个命令按钮,它通过 FileUpload 将发布的文件复制到指定位置。 Upto here everything works as expected.到目前为止,一切都按预期工作。 Now I have the code written on the same command button to read the excel file which is copied.现在我在同一个命令按钮上编写了代码来读取复制的 excel 文件。 The first file reads correctly.第一个文件正确读取。 while reading the second file it finds the same data.在读取第二个文件时,它会发现相同的数据。

Now to test it further.现在进一步测试它。 I have moved the file read code to a different command button.我已将文件读取代码移至不同的命令按钮。 Now I did 2 tests.现在我做了2次测试。 Test 1. Upload 2 files using file upload Click on command button 1 to copy the posted files to spefified location click on Command button 2 to loop through the files on copied folder and read the files.测试 1. 使用文件上传上传 2 个文件 点击命令按钮 1 将发布的文件复制到指定位置 点击命令按钮 2 循环复制文件夹中的文件并读取文件。 Result: Same issue, reading second file it gets the same data.结果:同样的问题,读取第二个文件它得到相同的数据。 Test 2: Run the application.测试 2:运行应用程序。 Do not select and upload any file.不要 select 和上传任何文件。 Manually copy the same to files to the specified location.手动将相同的文件复制到指定位置。 click the command button 2 to read the files.单击命令按钮 2 以读取文件。 File reads correctly.文件读取正确。

its very clear there is nothing wrong with the function to read files.很明显function读取文件没有问题。 When File Upload is working there must be some conflict in memory management.当文件上传工作时,memory 管理中肯定存在一些冲突。 I wonder whats my alternative to the requirement.我想知道我的替代要求是什么。

The issue has been resolved.问题已解决。 There's nothing wrong with the read excel.读取excel没有问题。 The problem is saving the posted file to specified location.问题是将发布的文件保存到指定位置。

protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
{
    HttpFileCollection files = Request.Files;
    for (int i = 0; i < files.Count; i++)
    {
        HttpPostedFile file = files[i];
        if (file.ContentLength > 0)
        {
            FileUploadExcel.SaveAs(Server.MapPath(@"~\UploadedFile\") + file.FileName);

        }
    }
string strPath = Path.GetFullPath(Server.MapPath(@"~\UploadedFile\"));
string[] strFiles = Directory.GetFiles(strPath);
foreach (string strFile in strFiles)
{
    ClsExcelUpload objExcelUpload = new ClsExcelUpload();
    string szConnectionString1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFile + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
  string result=  ReadExcel(szConnectionString1, strFile);

}

} }

        }
    }

it was reading and copying the same file with different name.So While reading from specified location it was reading same data from different files.它正在读取和复制具有不同名称的同一文件。因此,从指定位置读取时,它正在从不同文件中读取相同的数据。

the corrected function as follows修正后的 function 如下

        protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
    {
        List<string> LstStrPriExcelUploadError = new List<string>();
        HttpFileCollection files = Request.Files;
        foreach (HttpPostedFile uploadedFile in FileUploadExcel.PostedFiles)
        {
            string fn = System.IO.Path.GetFileName(uploadedFile.FileName);
            string SaveLocation = Server.MapPath(@"~\UploadedFile\") + "\\" + fn;

            uploadedFile.SaveAs(SaveLocation);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM