After the query gets OleDbDataReader, there are three ways to support data reading, as follows:
//Method One * * Speed mediumOleDbDataReader reader =command. ExecuteReader (); while(reader. Read ()) {varT1 = reader[0]; }//Method Two * * The slowest speedOleDbDataReader reader =command. ExecuteReader (); while(reader. Read ()) {varT1 = reader["Field name"]; }//Method Three * * Speed fastestOleDbDataReader reader =command. ExecuteReader (); while(reader. Read ()) {varT1 = reader. GetValue (0); }
For a description of the speed, let's take a quick look at the test.
There is a database, the table test in the database has 105945 rows of data, design a cyclic read to test their reading speed.
The test source is as follows:
Static voidMain (string[] args) { stringConnStr =string. Format ("provider=microsoft.jet.oledb.4.0;D ata source={0};","F:\\2.mdb"); stringS1 ="ID"; stringS2 ="Probe number"; stringS3 ="X"; stringS4 ="Y"; stringS5 ="H"; stringsql =string. Format ("SELECT {0},{1},{2},{3},{4} from Pipe point table", S1, S2, S3, S4, S5); TimeSpan time1=NewTimeSpan (); TimeSpan time2=NewTimeSpan (); TimeSpan Time3=NewTimeSpan (); intCount = -; //There are 105945 rows of tables queried in the database for(inti =0; I < count; i++) { using(OleDbConnection conn =NewOleDbConnection (CONNSTR)) { #regionConn. Open (); OleDbCommand Command=Conn. CreateCommand (); Command.commandtext=SQL; OleDbDataReader Reader=command. ExecuteReader (); DateTime Now=DateTime.Now; while(reader. Read ()) {varT1 = reader[0];varT2 = reader[1];varT3 = reader[2];varT4 = reader[3];varT5 = reader[4]; } time1= Time1 + (DateTime.Now-Now ); #endregion } } for(inti =0; I < count; i++) { using(OleDbConnection conn =NewOleDbConnection (CONNSTR)) { #regionConn. Open (); OleDbCommand Command2=Conn. CreateCommand (); Command2.commandtext=SQL; OleDbDataReader Reader=Command2. ExecuteReader (); DateTime Now=DateTime.Now; while(reader. Read ()) {varT1 = reader[s1];vart2 = Reader[s2];vart3 = Reader[s3];varT4 = Reader[s4];varT5 =READER[S5]; } time2= Time2 + (DateTime.Now-Now ); #endregion } } for(inti =0; I < count; i++) { using(OleDbConnection conn =NewOleDbConnection (CONNSTR)) { #regionConn. Open (); OleDbCommand Command3=Conn. CreateCommand (); Command3.commandtext=SQL; OleDbDataReader Reader=Command3. ExecuteReader (); DateTime Now=DateTime.Now; while(reader. Read ()) {varT1 = reader. GetValue (0);vart2 = reader. GetValue (1);vart3 = reader. GetValue (2);varT4 = reader. GetValue (3);varT5 = reader. GetValue (4); } Time3= Time3 + (DateTime.Now-Now ); #endregion}} Console.WriteLine (string. Format ("method one: \ r \ n Time: {0}s", time1. TotalSeconds)); Console.WriteLine (string. Format ("method two: \ r \ n Time: {0}s", Time2. TotalSeconds)); Console.WriteLine (string. Format ("method Three: \ r \ n Time: {0}s", Time3. TotalSeconds)); Console.readkey (); }
View Code
Loop 50 times the result, i.e. 50*105945:
100 Cycles Results: 100*105945:
OleDbDataReader Fast Data Read mode