Source code: usingSystem; usingSystem. collections. generic; usingSystem. data; usingSystem. data. sqlClient; usingSystem. linq; usingSystem. text; usingSystem. threading. tasks; namespaceSQLTest {classProgram {staticvoidMain (string [] args) {connection
Source code: using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. text; using System. threading. tasks; namespace SQLTest {class Program {static void Main (string [] args) {// connect
Source code:
Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. text; using System. threading. tasks; namespace SQLTest {class Program {static void Main (string [] args) {// connect to the database string connection = "server = Pan Shang \ SQLEXPRESS; database = db_test; trusted_Connection = true "; SqlConnection SC = new SqlConnection (); SC. connectionString = connection; try {s C. Open (); // Open the database connection Console. WriteLine ("the database connection has been enabled! "); SqlCommand cmd = new SqlCommand (); // create the SqlCommand object cmd. commandType = CommandType. text; // set the command to run the command cmd. connection = SC; // set the object property cmd. commandText = "insert into db_student (student_name, student_age, student_address, student_grade) VALUES (@ name, @ age, @ address, @ grade )"; // Add a parameter and assign it to cmd. parameters. add ("@ name", SqlDbType. varChar, 10 ). value = "pan"; cmd. parameters. add ("@ age", SqlDbType. int ). value = 19; cmd. Parameters. add ("@ address", SqlDbType. varChar ). value = "Wuhan"; cmd. parameters. add ("@ grade", SqlDbType. int ). value = 100; int I = cmd. executeNonQuery (); // execute the database add record command if (I> 0) Console. writeLine ("successfully added records"); // Console output add records} catch (Exception ex) {Console. writeLine ("Opening Database Error: {0}", ex. message);} finally {SC. close (); Console. writeLine ("the database connection is closed! ") ;}System. Console. ReadLine ();}}}
Database Design:
Run:
Database status after running: