usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {SqlConnection Sqlcon=NewSqlConnection ("Data source= (local);D atabase = Theatre_ms;uid =sa;pwd = 123;"); Try{Sqlcon. Open ();//database needs to be opened stringSqlstr ="INSERT into USER_TB values (' 1 ', ' 2 ', ' 1 ');"; SqlCommand cmd=NewSqlCommand (Sqlstr,sqlcon);//It is necessary to remember that cmd new requires two parameters, one is Sqlstr, the other is a database connection----> is used to indicate that it is doing that for that database.Cmd. ExecuteNonQuery ();//ExecuteNonQuery Returns the number of rows that are affectedConsole.WriteLine ("yes!"); } Catch(SqlException ex) {Console.WriteLine (ex). Message);//the output should be ex. Message} console.readline (); } }}
C # Learning Notes---database connections and exceptions