usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_2015_1_25{ Public Partial classFormsql:form { PublicFormsql () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //First Kindstring sqls = "server=.; database= operation; user=sa;pwd=123 "; SqlConnection conn = new SqlConnection (SQLS);//Database Connection ClassesSqlCommand cmd = conn. CreateCommand ();//Database Access Classescmd.commandtext = "Select *from student";//writing T-SQL statementsConn. Open ();//Open ConnectionSqlDataReader dr = cmd. ExecuteReader ();//perform access to return dataWhile (Dr. Read ())//reads a row of data through the read down pointer{ ListViewItem lt = new ListViewItem (); Lt. Text = dr["Sno"]. ToString (); Lt. SubItems.Add (dr["sname"). ToString ()); Lt. SubItems.Add (dr["Ssex"). ToString ()); Lt. SubItems.Add (dr["class"). ToString ()); ListView1.Items.Add (LT); } Conn. Close ();//Close Connection//The second Kind stringA ="server=.; database= work; user=sa;pwd=123"; SqlConnection b=NewSqlConnection (a); SqlCommand C=B.createcommand (); C.commandtext="Select*from Teacher"; B.open (); SqlDataReader D=C.executereader (); while(D.read ()) {ListViewItem F=NewListViewItem (); F.text= d["TNO"]. ToString (); F.subitems.add (d["Tname"]. ToString ()); F.subitems.add (d["Tsex"]. ToString ()); F.subitems.add (d["Tbirthday"]. ToString ()); F.subitems.add (d["Prof"]. ToString ()); F.subitems.add (d["Depart"]. ToString ()); LISTVIEW1.ITEMS.ADD (f); } b.close (); } }}
6. ListView (2)