Number of data in the query data path:
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) {///connection Database String connection = "server= Pinchamp \\sqlexpress;database=db_test; Trusted_connection=true "; SqlConnection sc = new SqlConnection (connection); Sc. ConnectionString = connection; try {sc. Open (); Open the Database connection Console.WriteLine ("Already Open database connection!"); SqlCommand cmd = new SqlCommand ("SELECT count (*) from Db_student", SC); int i = (int) cmd. ExecuteScalar ()///Command Console.WriteLine ("{0} data in table", i.ToString ());//start:3. Modify the code of the database data///////////// /* SqlCommand cmd = new SqlCommand ("UPDATE db_student SET Studen t_grade=99 where [email protected] ", SC); Create the SqlCommand object cmd. Parameters.Add ("@name", SqlDbType.VarChar). Value = "Pan"; int i = cmd. ExecuteNonQuery (); if (i > 0) Console.WriteLine ("Modified successfully!"); *///end:3. Code///////////////////////////////////////////////////////////start:1 to modify database data. Delete the database record code snippet///////////// /* String cmdtext = "DELETE from db_student WHERE [Email pro Tected] "; SqlCommand cmd = new SqlCommand (Cmdtext, SC); Cmd. Parameters.Add ("@name", SqlDbType.VarChar). Value = "Pan"; int i = cmd. ExecuteNonQuery (); if (i > 0) Console.WriteLine ("Delete record successfully!"); *///end:1. Deleting a database record code snippet///////////////////////////////////////////////////////////start:2. Add the code for the record//////////////// /* 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//end:2. Add record Code/////////////////////////////////////////////////////////////////} c Atch (Exception ex) {Console.WriteLine ("Open database error: {0}", ex. Message); } finally {SC. Close (); Console.WriteLine ("Database connection closed!"); } System. Console.ReadLine (); } }}
Operation Result: