C # Connect database-Once, delete, change, check

Source: Internet
Author: User

Connecting to a database

        Static voidMain (string[] args) {            //SqlConnection conn = new SqlConnection ();//instantiation of//Conn. ConnectionString = "server=.; Database=mydb;uid=sa;pwd=123 ";//The initialization-----can be executed with one of the following statementsSqlConnection conn =NewSqlConnection ("server=.; Database=mydb;uid=sa;pwd=123"); Console.WriteLine (Conn.            State); Conn. Open ();//Open a database connectionConsole.WriteLine (Conn.            State); Conn. Close ();//To close a database connectionConsole.WriteLine (Conn.                   State); }

Connect to a database and insert data

        Static voidMain (string[] args) {SqlConnection conn=NewSqlConnection ("server=.; Database=mydb;uid=sa;pwd=123"); Conn.            Open (); SqlCommand cmd=NewSqlCommand (); Cmd. Connection= conn;//Initialize, connect to database via Conn data ObjectCmd.commandtext ="INSERT into info values (' p005 ', ' student ', ' 0 ', ' n001 ', ' 1990-02-1 ')";//" " in the SQL statementcmd.            ExecuteNonQuery (); Conn.            Close (); Console.WriteLine ("OK"); }

Connect to the database and modify the data

         Public Static voidMain (string[] args) {SqlConnection conn=NewSqlConnection ("server=.; Database=mydb;uid=sa;pwd=123"); Conn.            Open (); SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; Cmd.commandtext="Update info set name= ' learner ' where code= ' p005 '"; Cmd.            ExecuteNonQuery (); Conn.        Close (); }

Connect to the database and delete the data

  public  static  void  Main (string  [] args) {Sq Lconnection conn  = "  server=.;                                           database=mydb;uid=sa;pwd=123   ); Conn.                        Open (); SqlCommand cmd  = Conn.            CreateCommand (); Cmd.commandtext  =  delete from info where code            = ' p005 '   " ; Cmd.            ExecuteNonQuery (); Conn.                   Close (); }

Connecting database query data

        Static voidMain (string[] args) {SqlConnection conn=NewSqlConnection ("server=.; Database=mydb;uid=sa;pwd=123"); Conn.            Open (); SqlCommand cmd=Conn.            CreateCommand (); Cmd.commandtext="SELECT * FROM Info"; SqlDataReader R= cmd. ExecuteReader ();//Query statement returns SqlDataReader class with ExecuteReader             while(R.read ()) {Console.WriteLine (r[0]. ToString () +r[1]. ToString () +r[2].            ToString ()); } conn.        Close (); }

Case: User Login implementation

         Public Static voidMain (string[] arges) {Console.Write ("User name:"); stringUID =Console.ReadLine (); Console.Write ("Password:"); stringPWD =Console.ReadLine (); SqlConnection Conn=NewSqlConnection ("server=.; Database=mydb;uid=sa;pwd=123"); Conn.            Open (); SqlCommand cmd=Conn.            CreateCommand (); Cmd.commandtext="SELECT * FROM login where username= '"+uid+"' and password= '"+pwd+"'"; SqlDataReader R=cmd.            ExecuteReader (); if(R.hasrows = =false) {Console.WriteLine ("yes, no."); }            Else{Console.WriteLine ("yes, right."); } conn.        Close (); }

*** . HasRows () differs from. Read (): HasRows only relational reading of data in a database is feasible and does not perform read operations. Read is performed to see if the execution is successful. Both return a variable of a bool row. * * *

C # Connect database-Once, delete, change, check

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.