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