Second, SqlCommand operation database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.