如何最小化OleDbDataAdapter从远程数据库获取数据的时间

时间:2014-09-10 13:25:48

标签: c# .net winforms oledb oledbdataadapter

我的Windows窗体应用程序包含OleDbDataAdapter,从远程数据库获取数据需要更长的时间。它无法检索/保存表格数据,如5000行(应用程序被触发)。这是我的代码。

environments = ConfigurationManager.GetSection("Environment") as NameValueCollection;
string strConnString = environments[envs];
conn = new OleDbConnection(strConnString);
conn.Open();
OleDbDataAdapter objDa = new OleDbDataAdapter("select * from tblABC", conn);
DataSet ds1 = new DataSet();
objDa.Fill(ds1);
dataGridView1.DataSource = ds1.Tables[0];

环境部分在app.config文件中配置:

<configuration>
  <configSections>
    <section name ="Environment" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <Environment>
    <add key ="CIT" value ="Password=pwd123;User ID=abc123;Data Source=db1;Persist Security Info=True;Provider=MSDAORA"/>
    <add key ="SIT" value ="Password=pwd234;User ID=abc234;Data Source=db2;Persist Security Info=True;Provider=MSDAORA"/>
    <add key ="UAT" value ="Password=pwd345;User ID=abc345;Data Source=db3;Persist Security Info=True;Provider=MSDAORA"/>

  </Environment>
</configuration>

如果有人能用代码建议更好的方法/机制,那将是非常棒的。

2 个答案:

答案 0 :(得分:3)

您是否尝试使用线程?在你的程序中的某个地方创建一个子功能,如下所示

public void dataPullingThread(){
    try{
        //your connection code goes here like below//
        environments = ConfigurationManager.GetSection("Environment") as NameValueCollection;
        string strConnString = environments[envs];
        conn = new OleDbConnection(strConnString);
        conn.Open();
        OleDbDataAdapter objDa = new OleDbDataAdapter("select * from tblABC", conn);
        DataSet ds1 = new DataSet();
        objDa.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        conn.Close();
    }
    catch (Exception e){

    }

}



//call your thread from desired location in program///

using System.Threading;
Thread thread = new Thread (new ThreadStart(dataPullingThread));
thread.start;

//Your application will continuously run; however, the data will appear when ever the thread auto kills itself. You can boost the speed if you create more then one thread. That means each thread selecting different rows of the database, I hope this information will help you//

答案 1 :(得分:2)

以下是一些常规的ADO.NET优化技巧:

  • 请确保您确实需要所有字段,而不是SELECT *。问题是可能会检索到许多未使用的字段值并消耗资源。

例如,如果您的表格包含的内容多于这三个字段,请执行SELECT Field1, Field2, Field3而不是SELECT *

  • 坚持以下连接打开/关闭模式:

示例:

using(var con = new OleDbConnection(strConnString))
{
    con.Open();

    ...

    con.Close();
}

即使发生了错误的事情,连接也会关闭,并且connection pooling机制将在服务器端使用。

  • DbDataReader对象要快得多。请尝试使用DbDataReader而不是DbDataAdapter。使用它来填充通用List,然后将DataGrid绑定到该List。

但是,您的连接本身看起来有问题。如何确定应用程序正在获取数据或尝试建立连接?要检查这一点,请将查询更改为非常快的查询,例如“从双选择sysdate”以检查问题是否来自连接尝试。