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

Source: Internet
Author: User
Tags readline

Connecting to a database

        static void Main (string[] args)        {            //sqlconnection conn = new SqlConnection ();//Instantiate            //conn. ConnectionString = "server=.; Database=mydb;uid=sa;pwd=123 ";//initialization-----can be executed with the following statement            SqlConnection conn = new SqlConnection (" server=.; Database=mydb;uid=sa;pwd=123 ");            Console.WriteLine (Conn. State);            Conn. Open ();//Opens the database connection            Console.WriteLine (Conn. State);            Conn. Close ();//Shut Down database connection            Console.WriteLine (Conn. State);                   }

Connect to a database and insert data

        static void Main (string[] args)        {            SqlConnection conn = new SqlConnection ("server=.; Database=mydb;uid=sa;pwd=123 ");            Conn. Open ();            SqlCommand cmd = new SqlCommand ();            Cmd. Connection = conn;//initialization, database cmd is connected by Conn data Object            . CommandText = "INSERT into info values (' p005 ', ' student ', ' 0 ', ' n001 ', ' 1990-02-1 ')"//"" In SQL statement            cmd. ExecuteNonQuery ();            Conn. Close ();            Console.WriteLine ("OK");        }

Connect to the database and modify the data

        public static void Main (string[] args)        {            SqlConnection conn = new SqlConnection ("server=.; Database=mydb;uid=sa;pwd=123 ");            Conn. Open ();            SqlCommand cmd = new SqlCommand ();            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)        {            SqlConnection conn = new SqlConnection ("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 void Main (string[] args)        {            SqlConnection conn = new SqlConnection ("server=.; Database=mydb;uid=sa;pwd=123 ");                        Conn. Open ();            SqlCommand cmd = conn. CreateCommand ();            Cmd.commandtext = "SELECT * from info";            SqlDataReader r = cmd. ExecuteReader ();//The query statement returns the SqlDataReader class while            (R.read ())            {                                Console.WriteLine (r[0]) with ExecuteReader. ToString () +r[1]. ToString () +r[2]. ToString ());            }            Conn. Close ();        }

Case: User Login implementation

        public static void Main (string[] arges)        {            console.write ("User name:");            String uid = Console.ReadLine ();            Console.Write ("Password:");            string pwd = Console.ReadLine ();            SqlConnection conn = new SqlConnection ("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 ("Hum, no");            }            else            {                Console.WriteLine ("Hum, 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.