2

我可以一次将一行添加到我的 Excel 电子表格中,但是速度非常慢(400 条记录需要 1 分钟,即使使用 Prepare 也是如此)。所以,我知道 Sql 是有效的并且 DataTable 是好的。

有效的代码:

    public void InsertFromDataTable(string strSql, DataTable dtTable, string strTableName)
    {
        if (m_oleDbHandler == null)
        {
            m_oleDbHandler = new OleDbHandler(m_strConnection);
        }

        //Do one row at a time since the DataAdapter did not work
        foreach (DataRow drRow in dtTable.Rows)
        {
            OleDbParmCollection cololedbParameters = new OleDbParmCollection();
            foreach (DataColumn dcColumn in dtTable.Columns)
            {
                OleDbParameter odpParameter = new OleDbParameter("@" + dcColumn.ColumnName, drRow[dcColumn.ColumnName]);
                odpParameter.ParameterName = "@" + dcColumn.ColumnName;
                odpParameter.DbType = OleDbHandler.GetDbType(dcColumn.GetType());
                odpParameter.Size = dcColumn.MaxLength;
                odpParameter.SourceColumn = dcColumn.ColumnName;

                cololedbParameters.Add(odpParameter);
            }

            m_oleDbHandler.ExecuteCommand(strSql, cololedbParameters, true);
        }
    }
}

当我尝试使用 DataAdapter 做同样的事情时,它说它返回 458 行,但电子表格中没有新行。失败的代码:

        //DataAdapter version
        OleDbParmCollection cololedbParameters = new OleDbParmCollection();
        foreach (DataColumn dcColumn in dtTable.Columns)
        {
            OleDbParameter odpParameter = new OleDbParameter();
            odpParameter.ParameterName = "@" + dcColumn.ColumnName;
            odpParameter.OleDbType = OleDbHandler.GetOleDbType(dcColumn.GetType());
            odpParameter.DbType = OleDbHandler.GetDbType(dcColumn.GetType());
            odpParameter.Size = dcColumn.MaxLength;
            odpParameter.SourceColumn = dcColumn.ColumnName;

            cololedbParameters.Add(odpParameter);
        }

        m_oleDbHandler.InsertFromDataTable(strSql, dtTable, cololedbParameters, strTableName);

接着:

    public int InsertFromDataTable(string strSql, DataTable dtTable, OleDbParmCollection cololeDbParameters, string strTableName)
    {
        //Set every row as added so that they will be inserted
        foreach (DataRow drRow in dtTable.Rows)
        {
            drRow.SetAdded();
        }

        //Update the output table
        int intRows = -1;
        try
        {
            OleDbCommand oleDbCommand = new OleDbCommand(strSql, OpenConnection());

            foreach (OleDbParameter oleDbParameter in cololeDbParameters)
            {
                if (oleDbParameter.Value == null)
                {
                    oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
                }
                else if (string.IsNullOrEmpty(oleDbParameter.Value.ToString()))
                {
                    oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    oleDbCommand.Parameters.Add(oleDbParameter);
                }
            }

            OleDbDataAdapter odaAdapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM " + strTableName, OpenConnection()));
            odaAdapter.InsertCommand = oleDbCommand;
            odaAdapter.MissingMappingAction = MissingMappingAction.Passthrough;
            odaAdapter.MissingSchemaAction = MissingSchemaAction.Error;
            odaAdapter.TableMappings.Add(strTableName, dtTable.TableName);

            foreach (DataColumn dcColumn in dtTable.Columns)
            {
                odaAdapter.TableMappings[0].ColumnMappings.Add(dcColumn.ColumnName, dcColumn.ColumnName);
            }

            intRows = odaAdapter.Update(dtTable);
        }
        catch (OleDbException ex)
        {
            LogStackTrace();
            LogToDb.LogException(ex, LogToDb.c_strAppError);
            LogToDb.LogMessage("OleDb error", "OleDbHandler.InsertFromDataTable error", strSql, LogToDb.c_intErrorLevelOleDb);
            CancelTransactionAndClose();
            throw;
        }
        finally
        {
            CloseConnection();
        }

        return (intRows);
    }

为什么我会得到 intRows = 458,但 Excel 文件中没有新行?

编辑:我刚刚做了一个测试,看看如果我导出到 Microsoft Access .mdb(而不是 Excel)会发生什么,结果告诉我一些事情。我得到 458 个空白行。所以,我怀疑我在 Excel 中得到了 458 个空白行。所以,现在的问题是为什么这些行都是空白的。

4

1 回答 1

0

明白了——错误在下面的部分。这适用于 ExecuteNonQuery,但在没有数据时很糟糕。

        foreach (OleDbParameter oleDbParameter in cololeDbParameters)
        {
            if (oleDbParameter.Value == null)
            {
                oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
            }
            else if (string.IsNullOrEmpty(oleDbParameter.Value.ToString()))
            {
                oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                oleDbCommand.Parameters.Add(oleDbParameter);
            }
        }

适用于 Access 和 Excel 的更正代码是:

    foreach (OleDbParameter oleDbParameter in cololeDbParameters)
    {
            oleDbCommand.Parameters.Add(oleDbParameter);
    }

还有第二个不那么严重的错误。我用了

OleDbHandler.GetOleDbType(dcColumn.GetType());

应该是

OleDbHandler.GetOleDbType(dcColumn.DataType);
于 2012-10-29T20:36:10.653 回答