1.ExecuteNonQuery: Execute non-query T-SQL statement
using (SqlCommand cmd = conn.) CreateCommand ()) { "insert INTO UserInfo (Id,username,userage,delflag) VALUES (11, ' Zhaofei ', 18,0) "; // setting up T-SQL statements int num = cmd. ExecuteNonQuery (); // executes the T-SQL statement and returns the number of rows affected Console.WriteLine (num);}
2.ExecuteScalar: Executes the query and returns the first column of the first row in the result set returned by the query
using (SqlCommand cmd = conn.) CreateCommand ()) { ""; // Query the employee table for all information about sex as a male Label1. Text = cmd. ExecuteScalar (). ToString (); // executes the query and returns the first column of data in the first row of the information}
3.ExecuteReader: Executes the T-SQL statement and returns a SqlDataReader reader
using (SqlCommand cmd = conn.) CreateCommand ()) { "select * FROM employee"; = cmd. ExecuteReader (); // executes a T-SQL statement and generates a SqlDataReader reader while (reader. Read ()) { listView1.Items.Add (reader[1]. ToString ()); } // reader[1]: reads data from column 2nd of the table Reader. Close ();}
Second, SqlCommand operation database