The data obtained by the SQL query statement is in sub-format, we also use SqlDataReader to do, and then use the IDataReader to receive the read, the following is the code:
//I want to query a user table information, the user has a name, password, information three columns//1. Define a list array of user types, the code for the UserInfo class is belowList<userinfo> UserInfo =NewList<userinfo>();//2. We want to read the data of the query statement and save it. Here we will use the IDataReader statement//instance of the database class, the code for the class is belowDB db =NewDB ();//Parsing Methods using(IDataReader Read=db.read ("SELECT * from UserInfo")) { while(read. Read ()) {UserInfo a=NewUserInfo (); A.user_name= read[0]. ToString (); A.USER_PASSWD= read[1]. ToString (); A.user_region= read[2]. ToString (); Userinfo.add (a); } }
Code for the UserInfo class:
Public class UserInfo { publicstring user_name{get; Set;} Public string user_passwd {get; Set;} Public string user_region{get; Set;} }
The code for the DB class:
Public classDB {//Database Operations//1. Connect to the database PublicSqlConnection Connect () {stringRode =@"Data source=kty;integrated security=sspi;initial Catalog=shuyunquan"; SqlConnection Con=NewSqlConnection (Rode); Con. Open (); returncon; } //database methods for executing statements PublicSqlCommand Command (stringSQL) {SqlCommand cmd=NewSqlCommand (SQL, connect ()); returncmd; } //methods for affecting the number of rows Public intExecute (stringSQL) { returncommand (SQL). ExecuteNonQuery (); } //methods to return query results PublicSqlDataReader Read (stringSQL) { returncommand (SQL). ExecuteReader (); } }
Inserting data obtained from SQL query statements into the list of lists