View code
1 Using System; 2 Using System. Data; 3 Using System. Data. sqlclient; 4 Using System. Collections. Generic; 5 Using System. LINQ; 6 Public Class Studymodel 7 { 8 Public Static Void Main () 9 { 10 String Strcon = " Server =.; database = blog; uid = sa; Pwd = 1; " ; 11 Sqlconnection con = New Sqlconnection (strcon ); 12 Sqlcommand cmd = New Sqlcommand (); 13 Cmd. Connection = Con; 14 Cmd. commandtext = " Select * from employee " ; 15 Con. open (); 16 Sqldatareader reader = Cmd. executereader (); 17 List <employee> List = New List <employee> (); 18 Employee P = Null ; 19 While (Reader. Read ()) 20 { 21 P = New Employee (); 22 P. Name = reader [ " Name " ]. Tostring (); 23 P. Age = convert. toint32 (Reader [ " Age " ]); 24 P. Sex = convert. toint32 (Reader [ " Sex " ]); 25 List. Add (P ); 26 } 27 VaR Result = From A In List 28 Where A. age> 30 29 Select A; 30 Foreach (Employee Q In Result) 31 { 32 Console. writeline (Q. Name ); 33 } 34 Reader. Close (); 35 Cmd. Dispose (); 36 Con. Close (); 37 Console. writeline ( " Table " + List. Count + " Data entries " ); 38 } 39 Public Class Employee 40 { 41 Private String Name; 42 Private Int Age; 43 Private Int Sex; 44 Public String Name 45 { 46 Get { Return This . Name ;} 47 Set { This . Name = Value ;} 48 } 49 Public Int Age 50 { 51 Get { Return This . Age ;} 52 Set { This . Age = Value ;} 53 } 54 Public Int Sex 55 { 56 Get { Return This . Sex ;} 57 Set { This . Sex = Value ;} 58 } 59 } 60 }
Using the principle that C # Is Object-Oriented, all objects are created as objects in the database, and fields in the table are attributes of objects. In this way, after connecting to the database, you only need to perform object operations on the data in the database;