usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;//Reference NamespacesnamespaceADO Database Additions and deletions to search {classProgram {Static voidMain (string[] args) { //1. Database connectionSqlConnection conn =NewSqlConnection ("server=.; Database=ado;user=sa;pwd=ray;"); //2. Database OperationSqlCommand cmd =Conn. CreateCommand (); Cmd.commandtext="SELECT * from Student"; //3, database connection channel openConn. Open (); //4. Database ExecutionSqlDataReader DRC =cmd. ExecuteReader (); if(DRC. HasRows) { while(DRC. Read ()) {stringGender = ((BOOL) drc[2]) ?"male":"female"; intAge=datetime.now.year-convert.todatetime (drc[3]). year; stringBirthday=convert.todatetime (drc[3]). ToString ("yyyy mm month DD Day"); stringScore=convert.todecimal (drc[4]). ToString ("#.##"); Console.WriteLine ("School Number: {0}, Name: {1}, Gender: {2}, Age: {3}, Birthday: {4}, score: {5}", drc[0],drc[1],gender,age,birthday,score); } } //5. Database connection Channel closedConn. Close (); Console.readkey (); } }}
Chapter 1. ADO Database Data query (multirow, multiple columns)