删除
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace Delete1 { class Program { static void Main(string[] args) { bool has = false;//默认has false Console.Write("请输入要删除的编号:"); string ucode = Console.ReadLine(); //先查询有无此数据 SqlConnection conn = new SqlConnection("server=.;database=Data0504;user=sa;pwd=123;"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from Users where Ucode=@a"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", ucode); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)//如果查到数据让has=true { has = true; } conn.Close(); if (has)//如果has=true删除成功 { //执行删除 Console.WriteLine("删除成功!"); } else { Console.WriteLine("未查到此编号数据,请确定编号是否正确!"); } Console.ReadLine(); } } }