Ado. NET 5-delete records using Command object---Shinepans

Source: Internet
Author: User

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 (connection); Sc.            ConnectionString = connection; try {sc.  Open ();                Open the Database connection Console.WriteLine ("Already Open database connection!");                String cmdtext = "DELETE from db_student WHERE [email protected]";                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!"); Add a record code///////* 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 *////add record code///////*/} catch (Exception ex) {Co Nsole. WriteLine ("Open database error: {0}", ex.)            Message); } finally {SC.               Close (); Console.WriteLine ("Database connection closed!");        } System.Console.ReadLine (); }    }}

Delete execution:




The state after deletion:





Summary: Pay attention to the correctness of Cmdtext

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.