C # ado basics (using for database operations)

Source: Internet
Author: User

1. using is used to operate databases. using is an abbreviation of resource release. It is used to implement the IDisposable interface (the interface for releasing object Resources is IDisposable) private void button2_Click (object sender, routedEventArgs e) {// The point used by the source field indicates that the local machine can use an IP address if it is another machine. (the local machine can also use 127.0.0.1) using (SqlConnection conn = new SqlConnection ("Data Source = 127.0.0.1; Initial Catalog = OrderDB; User ID = sa; Password = 123456789") {conn. open (); // Open the connection using (SqlCommand cmd = conn. createCommand () {// insert data cmd. commandText = "insert into admin (name, password, rank) values ('hello', '000000', 1)"; cmd. executeNonQuery ();} using (SqlCommand cmd = conn. createCommand () {// use cmd for the parameter. commandText = "select * from admin where rank = @ rank"; cmd. parameters. add (new SqlParameter ("@ rank", 2); // because SqlDataReader implements the IDisposable interface, the interface for releasing object Resources is IDisposable using (SqlDataReader reader = cmd. executeReader () {// the query results are not stored in the database on the client. You can use DataSet to process while (reader. read () {string name = (string) reader. getString (0); MessageBox. show (name );}}}}}

Related Article

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.