Private stringlink ="server=.; Database=list;user=sa;pwd=123"; Public voidChaxun ()//Create a query function{SqlConnection Coon=NewSqlConnection (link);//connecting to a databaseCoon. Open ();//Open DatabaseSqlCommand cmd = Coon. CreateCommand ();//Create commandCmd.commandtext ="SELECT * FROM ListView";//Write command contentSqlDataReader dr = cmd. ExecuteReader ();//Execute Command intindex =0;//define the ListView. Index of itemsListView1.Items.Clear ();//first empty the listview1.items; while(Dr. Read ())//cyclic condition, with data inside{//Adds a single piece of data to the collection of Listview.itemsLISTVIEW1.ITEMS.ADD (dr["Code"]. ToString ());//items are represented by each columnListview1.items[index]. SubItems.Add (dr["name"]. ToString ());//SubItems Other columnsListview1.items[index]. SubItems.Add (dr["Pass"]. ToString ()); Index++; } cmd. Dispose (); Coon. Close (); }
Private voidButton1_Click (Objectsender, EventArgs e)//Enquiry{SqlConnection Coon=NewSqlConnection (link);//connecting to a databaseCoon. Open ();//Open DatabaseSqlCommand cmd = Coon. CreateCommand ();//Create commandCmd.commandtext ="SELECT * FROM ListView";//Write command contentSqlDataReader dr = cmd. ExecuteReader ();//Execute Command intindex =0;//define the ListView. Index of itemsListView1.Items.Clear ();//first empty the listview1.items; while(Dr. Read ())//cyclic condition, with data inside{//Adds a single piece of data to the collection of Listview.itemsLISTVIEW1.ITEMS.ADD (dr["Code"]. ToString ());//items are represented by each columnListview1.items[index]. SubItems.Add (dr["name"]. ToString ());//SubItems Other columnsListview1.items[index]. SubItems.Add (dr["Pass"]. ToString ()); Index++;//index + +, recycle} cmd. Dispose ();//Command EmptyCoon. Close ();//Database Shutdown}
Private voidButton2_Click (Objectsender, EventArgs e)//Delete { if(listview1.selecteditems.count>0)//if the number of selected data to be deleted is greater than 0, the deletion is not performed, otherwise{stringSCODE = listview1.selecteditems[0]. Text.tostring ();//create a variable to receive the text content of the first column indexSqlConnection Coon =NewSqlConnection (link);//connecting to a databaseCoon. Open ();//Open DatabaseSqlCommand cmd = Coon. CreateCommand ();//Create commandCmd.commandtext ="Delete from ListView where Code= '"+ SCODE +"'";//Write command contentCmd. ExecuteNonQuery ();//Execute CommandCmd. Dispose ();//Command EmptyCoon. Close ();//Database ShutdownChaxun ();//Execute Query function } }
Private void button3_click (object// Home Page Add button { new Add (); // popup Add box }
Private voidButton1_Click (Objectsender, EventArgs e)//Increase{if(textbox2.text!=NULL&&textbox3.text!=NULL)//If the text field is not empty to perform an increase, the dot increase does not respond { stringCode=textbox1.text;//text in first column stringName=textbox2.text;//text in the second column stringPass=textbox3.text;//text in the third columnSqlConnection Coon =NewSqlConnection ("server=.; Database=list;user=sa;pwd=123");//connecting to a databaseCoon. Open ();//Open DatabaseSqlCommand cmd = Coon. CreateCommand ();//Create commandCmd.commandtext ="INSERT into ListView values ('"+name+"', '"+pass+"')";//Write command StatementsCmd. ExecuteNonQuery ();//Execute CommandCmd. Dispose ();//Empty CommandCoon. Close ();//Close the databaseMessageBox.Show ("Increase Success"); } }
Private voidButton4_Click (Objectsender, EventArgs e)//Homepage Modify button { if(listview1.selecteditems.count>0)//Select what you want to modify to perform the modification { stringCode = listview1.selecteditems[0]. Text;//assigns the contents of the first column of the selected text to codeUpdate F =NewUpdate (code);//The code is thrown in so that the modified form receives the code value (right-click to define)f.show (); } }
Private stringCode; PublicUpdatestringCode//passes the code value of the selected contents of the main page to the form update and appears in the first column of text in the form update{InitializeComponent (); Code= code;//Pass ValueTextBox1.Text = Code;//Show } Private voidButton1_Click (Objectsender, EventArgs e)//Modify { if(textbox2.text!=NULL&&textbox3.text!=NULL)//modifications are performed only if the text column contents are not empty { stringname = TextBox2.Text;//second column text stringpass = TextBox3.Text;//third column TextSqlConnection Coon =NewSqlConnection ("server=.; Database=list;user=sa;pwd=123");//connecting to a databaseCoon. Open ();//Open DatabaseSqlCommand cmd = Coon. CreateCommand ();//Create commandCmd.commandtext ="Update listview Set Name= '"+name+"', pass= '"+pass+"' where code="+code+"";//Write command StatementsCmd. ExecuteNonQuery ();//Execute CommandCmd. Dispose ();//Empty CommandCoon. Open ();//Close the databaseMessageBox.Show ("Modification succeeded"); } }
Check and delete of ListView connection database