Professional C# 2nd Edition

源代码在线查看: indexertest.cs

软件大小: 4420 K
上传用户: newyearday
关键词: Professional Edition 2nd
下载地址: 免注册下载 普通下载 VIP

相关代码

				using System;
				using System.Data.SqlClient;
				
				public class SimpleSelect
				{
					public static int Main(string[] args)
					{
						string        source = Login.Connection ;
				
						string        select = "SELECT CategoryID,CategoryName FROM Categories" ;
				
						using ( SqlConnection conn = new SqlConnection ( source ) )
						{
							conn.Open() ;
				
							SqlCommand    cmd = new SqlCommand ( select , conn ) ;
				
							SqlDataReader aReader = cmd.ExecuteReader ( ) ;
				
							aReader.Read ( ) ;
				
							// Now time access to the first field in the reader...
							long timeInitial , timeElapsed , iterations , maxIterations = 1000000;
				
							int   theCategoryID ;
				
							timeInitial = DateTime.Now.Ticks ;
				
							for ( iterations = 0 ; iterations < maxIterations ; iterations++ )
								theCategoryID = (int) aReader[0] ;
				
							timeElapsed = DateTime.Now.Ticks ;    
				
							Console.WriteLine ( "{0} iterations using numeric indexer : {1}s" , 
								iterations ,
								new TimeSpan ( timeElapsed - timeInitial ) ) ;
				
							timeInitial = DateTime.Now.Ticks ;
				
							for ( iterations = 0 ; iterations < maxIterations ; iterations++ )
								theCategoryID = (int) aReader["CategoryID"] ;
				
							timeElapsed = DateTime.Now.Ticks ;    
				
							Console.WriteLine ( "{0} iterations using textual indexer : {1}s" , 
								iterations ,
								new TimeSpan ( timeElapsed - timeInitial )  ) ;
				
							timeInitial = DateTime.Now.Ticks ;
				
							for ( iterations = 0 ; iterations < maxIterations ; iterations++ )
								theCategoryID = aReader.GetInt32 (0);
				
							timeElapsed = DateTime.Now.Ticks ;    
				
							Console.WriteLine ( "{0} iterations using GetInt32(0)     : {1}s" , 
								iterations ,
								new TimeSpan ( timeElapsed - timeInitial )  ) ;
				
							conn.Close ( ) ;
				
							return 0;
						}
					}
				}
							

相关资源