繁体   English   中英

C#-使用OleDB加速从Excel读取

[英]C# - Speeding up reading from Excel using OleDB

我正在尝试在C#中使用OleDB读取Excelfile并将其放入列表框。 我遵循了很多指南,并且有效! 但是,它始终以50毫秒的延迟显示数据。 这个很小,但是我正在为我的一些同事做这个,我希望它能更好,更流畅地工作,即减少延迟。 excel文件不是很大,它包含约840个项目,因此我可以想象它会更快。

首先,我将向您展示将数据填充到列表框中的代码:

    const int straatmeubilair = 0;
    const int boomproducten = 1;
    const int dekkenEnBruggen = 2;
    String queryTempCategorie;
private void lbCategorie_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (lbCategorie.SelectedIndex)
        {
            case straatmeubilair:
                queryCategorie = "A%";
                break;

            case boomproducten:
                queryCategorie = "B%";
                break;

            case dekkenEnBruggen:
                queryCategorie = "C%";
                break;
        }

        if (queryTempCategorie != queryCategorie)
        {
            queryTempCategorie = queryCategorie;
            updateTable(dbProductInfo.getData("SELECT DISTINCT [Productfamilie] FROM [Bestelinfo$] WHERE [Pagina] LIKE '" + queryCategorie + "' ORDER BY [Productfamilie] ASC"), lbFamilie, "Productfamilie");
        }
    }

private void updateTable(DataTable tempTable, ListBox tempListbox, String column)
    {
        tempListbox.DataSource = tempTable;
        tempListbox.DisplayMember = column;
    }

因此,当列表框更改索引时,它会尝试使用数据库请求的数据表填充另一个列表框。 它还具有一个额外的if / else语句,以在他的最后一个请求相同时阻止其请求数据。 下面的代码显示了对Excel的数据请求。

public DataTable getData(String query)
    {
        // Test features
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        Console.WriteLine(testAmount);
        testAmount += 1;
        // Clear the datatable
        Console.WriteLine("CLEARING DATATABLE");
        DataTable data = new DataTable();
        // Create the connectionstring
        strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + databasePathFile + @";Extended Properties=""Excel 12.0 xml;HDR=Yes;IMEX=1""";
        // Create adapter
        Console.WriteLine("CREATING ADAPTER");
        adapter = new OleDbDataAdapter();
        // Create connection
        Console.WriteLine("CREATING CONNECTION");
        connection = new OleDbConnection(strConnection);
        // Create the command for the given connection
        Console.WriteLine("CREATING COMMAND");
        command = new OleDbCommand(query, connection);

        try
        {
            // Open the connection
            connection.Open();
            // Give the command to the adapter
            adapter.SelectCommand = command;
            // Fill the dateset with the adapter
            adapter.Fill(data);
        }
        catch (Exception e)
        {
            MessageBox.Show("Something went wrong, contact IT");
            Console.WriteLine(e.Message);
        }
        finally
        {
            // Close connection
            connection.Close();
            // Dispose of adapter
            adapter.Dispose();
            TimeSpan ts = stopwatch.Elapsed;
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            Console.WriteLine("Runtime " + elapsedTime);
        }
        return data;
    }

注意:连接字符串中的“ databasePathFile”是在程序的前面定义的。

我认为我在做些奇怪的事情,这总是让我延迟。 有没有人有什么建议? (类似:“您需要完全更改您的方法”也可以:P我仍在学习!但是我想保留此方法。

只是丢了我的两分钱,但尝试在单独的线程中运行getData函数。 我指的是-> System.Threading。

通常,在多个线程上运行您的应用程序将使进程运行更快,并且用户体验更加无缝。

如果您不熟悉C#中的线程,我将提供示例代码。

希望以上内容对您有所帮助。

暂无
暂无

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

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