无法转换#System; System.Data.DataRow'类型的对象输入' System.String'

时间:2017-10-10 20:09:13

标签: c# arraylist casting

我正在尝试比较两个Exel文件,但无法解决异常问题"无法转换类型&System; System.Data.DataRow'输入' System.String'。" 以下代码例外。

问题是在交叉两个文件之后,交叉数据没有被添加到下面的交叉点变量中。

try
{
    foreach (var i in existingLead.Cast<string>().Intersect(newLead.Cast<string>()))
    {
        intersection.Add(i);
    }
}
catch (Exception e)
{
    MessageBox.Show(e.Message, "In Intersection Array", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

完整的课程在这里。此类在Form1类On Button Click中启动。用户选择两个excel文件后。它从文件路径的相应文本框中获取文件路径

class Compare
{
    public ArrayList existingLead = new ArrayList();
    public ArrayList newLead = new ArrayList();
    public ArrayList intersection = new ArrayList();

    public void FindDuplicates(string filePathExisting, string filePathNew)
    {
        List<DataTable> existingDataTableList = ImportExcel(filePathExisting);
        List<DataTable> newDataTableList = ImportExcel(filePathNew);

        foreach (var item in existingDataTableList)
        {
            foreach (DataRow row in item.Rows)
            {
                //add to array
                existingLead.Add(row);
            }

        }

        foreach (var item in newDataTableList)
        {
            foreach (DataRow row in item.Rows)
            {
                //add to array
                newLead.Add(row);
            }

        }

        try
        {
            foreach (var i in existingLead.Cast<string>().Intersect(newLead.Cast<string>()))
            {
                intersection.Add(i);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "In Intersection Array", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }



        }

    private List<DataTable> ImportExcel(string FileName)
    {
        List<DataTable> _dataTables = new List<DataTable>();
        string _ConnectionString = string.Empty;
        string _Extension = Path.GetExtension(FileName);
        //Checking for the extentions, if XLS connect using Jet OleDB
        if (_Extension != null)
        {
            if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
            {
                _ConnectionString =
                    "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0";
            }
            //Use ACE OleDb
            else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase) || _Extension != null)
            {
                _ConnectionString =
                    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0";
            }
            else
            {
                MessageBox.Show("File extensoin must be .xls or .xlsx", "Incompatible File Type", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        DataTable dataTable = null;

        using (OleDbConnection oleDbConnection =
            new OleDbConnection(string.Format(_ConnectionString, FileName)))
        {
            if (oleDbConnection != null)
            {
                try
                {
                    oleDbConnection.Open();
                    //Getting the meta data information.
                    //This DataTable will return the details of Sheets in the Excel File.
                    DataTable dbSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
                    foreach (DataRow item in dbSchema.Rows)
                    {
                        //reading data from excel to Data Table
                        using (OleDbCommand oleDbCommand = new OleDbCommand())
                        {
                            oleDbCommand.Connection = oleDbConnection;
                            oleDbCommand.CommandText = string.Format("SELECT * FROM [{0}]",
                                item["TABLE_NAME"].ToString());
                            using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter())
                            {
                                oleDbDataAdapter.SelectCommand = oleDbCommand;
                                dataTable = new DataTable(item["TABLE_NAME"].ToString());
                                oleDbDataAdapter.Fill(dataTable);
                                _dataTables.Add(dataTable);
                            }
                        }
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show( e.Message, "Querying Data Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("Connection String Empty", "No Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        return _dataTables;
    }

1 个答案:

答案 0 :(得分:0)

在强制转换和数据行实例之间:

existingLead.Cast<string>() ...

告诉它你想要哪个数据行

existingLead["ColName"].Cast<string>() ...

或existsinglead.item(&#34; ColName&#34;)或者DataRow会这样做。