【问题标题】:WPF C# Access Insert OptimizationWPF C# 访问插入优化
【发布时间】:2016-11-11 21:22:14
【问题描述】:

我必须在 MS Access 中插入大约 25,000 行,而且花费的时间太长(3 小时 +-),有什么方法可以优化它吗?

首先我检查数据库中是否存在寄存器,如果不存在我插入,否则我更新。现在我正在测试只是插入(第一次,表是空的)。在插入之前,我必须格式化一些数据。

conn = new OleDbConnection(Conexao.getConexao());
conn.Open();


foreach (Pendencia pendencia in listaPendeciaNassau)
{
    if (!PendenciaNassauExisteVerificar(pendencia.PendenciaId))
    {

    if (!String.IsNullOrEmpty(pendencia.Contrato))
    {
        //Insert na tabela do BD
        OleDbCommand cmd = new OleDbCommand("INSERT INTO tblImportacao( " +
                                                                        " Contrato, " +
                                                                        " Tipo, " +
                                                                        " PendenciaNivel, " +
                                                                        " PendenciaNassauId, " +
                                                                        " PendenciaTipo, " +
                                                                        " GarantiaDescricao, " +
                                                                        " Observacao, " +
                                                                        " AberturaData, " +
                                                                        " VencimentoData, " +
                                                                        " CarenciaInicio, " +
                                                                        " CarenciaFim, " +
                                                                        " DiasDecorridos, " +
                                                                        " DiasPendentes, " +
                                                                        " Produto, " +
                                                                        " ClienteGrupo, " +
                                                                        " ClienteNome, " +
                                                                        " Rating, " +
                                                                        " ClienteCnpj, " +
                                                                        " Officer, " +
                                                                        " CentroCusto, " +
                                                                        " Moeda, " +
                                                                        " OperacaoValor, " +
                                                                        " LiquidacaoData, " +
                                                                        " PendenciaIdComposto, " +
                                                                        " Lastro, " +
                                                                        " Corretora, " +
                                                                        " AdAm, " +
                                                                        " MnMe, " +
                                                                        " PendenciaTipoMacro, " +
                                                                        " Segmento, " +
                                                                        " PendenciaOrigem, " +
                                                                        " ImportacaoData, " +
                                                                        " PorContrato" +
                                                                        ") " +
                                                       "VALUES (" + "'" + pendencia.Contrato + "'" +
                                                                        "," + "'" + pendencia.Tipo + "'" +
                                                                        "," + "'" + pendencia.PendenciaNivel + "'" +
                                                                        "," + "'" + pendencia.PendenciaId + "'" +
                                                                        "," + "'" + pendencia.PendenciaTipo + "'" +
                                                                        "," + "'" + pendencia.GarantiaDescricao + "'" +
                                                                        "," + "'" + pendencia.Observacao + "'" +
                                                                        "," + (String.IsNullOrEmpty(pendencia.AberturaData.Trim()) ? "null" : "#" + pendencia.AberturaData + "#") +
                                                                        "," + (String.IsNullOrEmpty(pendencia.VencimentoData.Trim()) ? "null" : "#" + pendencia.VencimentoData + "#") +
                                                                        "," + (String.IsNullOrEmpty(pendencia.CarenciaInicioData.Trim()) ? "null" : "#" + pendencia.CarenciaInicioData + "#") +
                                                                        "," + (String.IsNullOrEmpty(pendencia.CarenciaFimData.Trim()) ? "null" : "#" + pendencia.CarenciaFimData + "#") +
                                                                        "," + "'" + pendencia.DiasDecorridos + "'" +
                                                                        "," + "'" + pendencia.DiasPendentes + "'" +
                                                                        "," + "'" + pendencia.Produto + "'" +
                                                                        "," + "'" + pendencia.ClienteGrupo + "'" +
                                                                        "," + "'" + pendencia.ClienteNome + "'" +
                                                                        "," + "'" + pendencia.Rating + "'" +
                                                                        "," + "'" + pendencia.ClienteCnpj + "'" +
                                                                        "," + "'" + pendencia.Officer + "'" +
                                                                        "," + "'" + pendencia.CentroCusto + "'" +
                                                                        "," + "'" + pendencia.Moeda + "'" +
                                                                        "," + "'" + (String.IsNullOrEmpty(pendencia.OperacaoValor) ? String.Empty : RetornarValorMonetarioFormatado(pendencia.OperacaoValor)) + "'" +
                                                                        "," + (String.IsNullOrEmpty(pendencia.LiquidacaoData.Trim()) ? "null" : "#" + pendencia.LiquidacaoData + "#") +
                                                                        "," + "'" + pendencia.PendenciaIdComposto + "'" +
                                                                        "," + "'" + pendencia.Lastro + "'" +
                                                                        "," + "'" + pendencia.Corretora + "'" +
                                                                        "," + "'" + pendencia.AdAm + "'" +
                                                                        "," + "'" + pendencia.MnMe + "'" +
                                                                        "," + "'" + pendencia.PendenciaTipoMacro + "'" +
                                                                        "," + "'" + pendencia.Segmento + "'" +
                                                                        "," + "'PGPNassau'" +
                                                                        ",#" + DateTime.Now + "#" +
                                                                        "," + "'" + pendencia.PorContratoDescricao + "'" +
                                                                        ");", conn);
        cmd.ExecuteNonQuery();
    }
   }
}

private bool PendenciaNassauExisteVerificar(int pendenciaId)
{
    bool existe = false;

OleDbConnection conn = null;


#region Select e conversão do DataSet
try
{
    conn = new OleDbConnection(Conexao.getConexao());
    conn.Open();

    //Select da tabela tblPendencia
    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * " +
                                                 "FROM tblImportacao I" +
                                               " WHERE I.pendenciaNassauId = " + pendenciaId, conn);

    DataSet ds = new DataSet();
    da.Fill(ds, "tblPendencia");
    DataTable dt = new DataTable();
    dt = ds.Tables["tblPendencia"];


    if (ds.Tables != null && ds.Tables[0].Rows.Count > 0)
    {
        existe = true;
    }


}

catch (Exception ex)
{
    EventLog.WriteEntry(APLICACAO, ex.Message, EventLogEntryType.Error, 234);
}

finally { conn.Close(); }

return existe;

#endregion
}

【问题讨论】:

  • PendenciaNassauId 上是否有索引?如果不是,当您插入第 25000 行时,数据库必须检查 24999 其他行,如果它们恰好是这一行。当您插入第 24999 行时,它必须检查 24998 行......等等。还有其他需要优化的东西(比如,如果一个简单的Command.ExecuteScalar 就足够了,就不要使用完整的DataAdapter)但这是首先要检查的。
  • 如果您总是插入一个空表,您还可以在开始插入之前简单地消除源数据中的重复项,这肯定会加快速度。如果您的源数据没有重复,但目标中可能存在冲突行,您可以从目标中选择所有现有 ID,然后删除源中的所有匹配行。最重要的是,尽量减少实际插入的操作。
  • @Denis - 有人可能对您的问题投了反对票,因为到目前为止您还没有回复任何 cmets。你寻求帮助,一些人提出了建议,你没有表现出任何“帮助他们帮助你”的倾向。一些 Stack Overflow 用户可能会觉得“f...烦人”。
  • 顺便说一句,没有人通过发表评论获得声誉。信不信由你,人们只是想帮助你。如果您真的喜欢冒犯他人,请不要怀疑您是否会被否决或没有帮助。此外,您不提供minimal reproducible example。您只需发布代码并尖叫帮助。同样对于企业程序员来说,为什么坚持使用 MS Access 作为数据库非常令人难以置信(另请参阅我发布的链接中的@bubi 答案)。这只是一个有根据的猜测,为什么你会被否决。而且投票会损害声誉!!!

标签: c# wpf ms-access


【解决方案1】:

经过一段时间的思考后,我们决定最好的办法是在本地机器上制作数据库的副本,在其中工作,然后将其复制回服务器。现在整个过程大约需要 15 分钟!

有时仅仅编码并不是解决方案。

感谢大家的努力!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2018-04-08
    相关资源
    最近更新 更多