搜索特定目录中的Excel文件错误 - "无法找到可安装的ISAM"

时间:2017-08-19 13:22:14

标签: c# desktop-application

使用断点我发现问题在搜索过程达到dAdapter.Fill(ds);时开始。例外情况是"无法找到可安装的ISAM"。

function starts here
{
    try
    {
        string startPath = directorytbx.Text.ToString() + @":\Windows Data Management System\";
        // string[] oDirectories = Directory.GetDirectories(startPath, nametbx.Text + ".xls", SearchOption.AllDirectories);
        DirectoryInfo info = new DirectoryInfo(startPath);
        DirectoryInfo[] arry = info.GetDirectories();
        List<string> listOfFolderNames = new List<string>();
        foreach (DirectoryInfo dInfo in arry)
        {
            listOfFolderNames.Add(dInfo.Name);
        }
        List<string> finalPaths = new List<string>();
        foreach (string checker in listOfFolderNames)
        {
            if (checker.ToUpper().Contains(nametbx.Text.ToUpper()))
            {
                finalPaths.Add(checker);
            }
        }
        int i = 0;
        foreach (DirectoryInfo dInfo in arry)
        {
            DirectoryInfo dsubInfo = new DirectoryInfo(dInfo.FullName + "\\");
            FileInfo[] dsubInfoArray = dsubInfo.GetFiles();

            String name = dsubInfo + "\'" + dsubInfoArray[1].ToString();

            //String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +dsubInfoArray + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

            //string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dsubInfoArray + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

            //create the "database" connection string 
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + name + ";Excel 12.0 Xml;HDR=YES";

            //create the database query
            string query = "SELECT * FROM [Sheet1$]";

            //create an OleDbDataAdapter to execute the query
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
            DataSet ds = new DataSet();
            dAdapter.Fill(ds);
            //fill the DataTable
            DataTable data = ds.Tables[0];
            dAdapter.Fill(data);
            dataGridView1.DataSource = data;
            dAdapter.Dispose();

            i++;
        }
    }
    catch (Exception ex)
    {
        errlbl.Show();
        errlbl.Text = "Error: " + ex.Message;
    }
}

0 个答案:

没有答案