簡體   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