usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;//Reference NamespacesnamespaceDelete and change the search {classProgram {Static voidMain (string[] args) {
//Add: Insert into //1. Connect to the databaseSqlConnection con =NewSqlConnection ("server=; database=ado; user=sa; Pwd=ray ;"); //2. Writing SQL statements stringsql ="INSERT into Student values (' Money eight ', 1, ' 1990-01-05 ', ' all ', ' 22050219900105061X ', ' 18853315510 ')"; //3. Create an object that executes an SQL statement using(SqlCommand cmd =NewSqlCommand (sql, con)) {con. Open (); //4. Open the connection intz = cmd. ExecuteNonQuery ();//5. Execute the statementConsole.WriteLine ("{0} row data was successfully inserted \ n"+sql, z); Con. Close (); //6. Close the connectionConsole.readkey (); } } }}
//Delete : DeleteSqlConnection con =NewSqlConnection ("server=; database=ado; user=sa; Pwd=ray ;"); stringsql ="Delete from Student where sname= ' money eight '"; using(SqlCommand cmd =NewSqlCommand (sql, con)) {con. Open (); ints =cmd1. ExecuteNonQuery (); Console.WriteLine ("{0} row data was successfully deleted \ n"+SQL, s); Con. Close (); Console.readkey (); }
//(3) Change: Update...setSqlConnection con =NewSqlConnection ("server=; database=ado; user=sa; Pwd=ray ;"); stringsql ="Update Student set sname= ' Zhangxiao ', sbirthday= ' 2000-01-01 ', sscore=100 where scode=10001"; using(SqlCommand cmd=NewSqlCommand (Sql,con)) {con. Open (); intg =cmd. ExecuteNonQuery (); Console.WriteLine ("the {0} row data was successfully changed \ n"+sql,g); Con. Close (); Console.readkey (); }
//(4) Check: Select
SqlConnection con =NewSqlConnection ("server=; database=ado; user=sa; Pwd=ray ;"); stringsql ="Select COUNT (*) from Student"; using(SqlCommand cmd=NewSqlCommand (Sql,con)) {con. Open (); ObjectC=cmd. ExecuteScalar (); Console.WriteLine ("there are {0} data in the student table", c); Con. Close (); Console.readkey (); }
Chapter 1. ADO database additions and deletions change