in--sql
--Create Ren's database and insert a messageCreate DatabaseRenGo Use RenGoCreate TableXinxi(Codenvarchar( -)Primary Key,--numberingNamenvarchar( -)--name)Insert intoXinxiValues('1001','Zhangsan')
In the previous operation, there is a bug, in the operation of "pruning", if the user lost the wrong number, no reminder, then you have to query whether there is this number of information before the operation
in//c#
//Modify for (; ; ) { //first query whether there is such a data, if any, to operate; if not, re-enter, know that the input is correct so far!! BOOLb =false;//using intermediate variablesConsole.Write ("Please enter the number you want to modify:"); stringNo =Console.ReadLine (); //Search ShowSqlConnection zhancnn =NewSqlConnection ("server=.; Database=ren;user=sa;pwd=123");//Connection//Statement of OperationSqlCommand Zhancmd =zhancnn. CreateCommand (); Zhancmd.commandtext="SELECT * from Xinxi where code= '"+ No +"'"; //statements that perform actionszhancnn. Open (); SqlDataReader SS=Zhancmd. ExecuteReader (); if(SS. HasRows)//whether there is data to be modified in the database{b=true; } zhancnn. Close (); if(b = =true)//If you have the data you want to modify{Console.WriteLine ("Find the ""+ No +""Information, are you sure you want to modify it? y/n"); if(Console.ReadLine (). ToUpper () = ="Y")//Determining Modifications{Console.Write ("Please enter the name you want to modify:"); stringMingzi =Console.ReadLine (); Zhancmd.commandtext="Update Xinxi set name= '"+ Mingzi +"' where code= '"+ No +"'";//since the above is instantiated, do not re-instantiate, note open and closezhancnn. Open (); Zhancmd. ExecuteNonQuery (); Zhancnn. Close (); Console.WriteLine ("number is"+ No +"the information modified successfully! "); } Else//Do not modify { } Break; } Else//If you don't have the data you want to modify{Console.WriteLine ("The database does not have this message, please enter the correct code!! "); }} console.readline ();
Modify and delete the same format, the execution statement is not the same! !
SQL and C # combine complete modifications to delete information