Source:
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) {//Connection database String connection = "server= Pinchamp \\sqlexpress;database=db_test; Trusted_connection=true "; SqlConnection sc = new SqlConnection (); Sc. ConnectionString = connection; try {sc. Open (); Open the Database connection Console.WriteLine ("Already Open database connection!"); SqlCommand cmd = new SqlCommand ();//Create SqlCommand object cmd. CommandType = CommandType.Text; Sets the Execute text command cmd. Connection = SC; Sets the object property of CMD. CommandText = "INSERT into Db_student (student_name,student_age,student_address,student_grade) VALUES (@n Ame, @age, @address, @grade) "; Add a parameter and assign a value cmd to the parameter. 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 database Add record command if (i > 0) Console.WriteLine ("Add record Success"); Console output add record} catch (Exception ex) {Console.WriteLine ("Open database error: {0}", ex.m Essage); } finally {SC. Close (); Console.WriteLine ("Database connection closed!"); } System.Console.ReadLine (); } }}
Database design:
Run:
Database state after running: