完整修改和删除:
当你输入了要删除的用户名,先提示有没有此条数据
先查
后删/后改
------------
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace 完整删除_修改_攻击防攻击 { class Program { static void Main(string[] args) { Console.Write("请输入要删除的用户名:"); string name = Console.ReadLine(); SqlConnection conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123"); SqlCommand com = conn.CreateCommand(); com.CommandText = "select *from Users where usename='"+name+"'"; conn.Open(); SqlDataReader dr=com.ExecuteReader(); if (dr.HasRows)//如果查到此用户信息 { Console.Write("是否删除该用户:(Y/N)");//如果有此条信息,那么需要进行删除判断 string n = Console.ReadLine();//记录用户的操作 if (n.ToUpper() == "Y")//判断用户操作,如果是Y,说明要删除 { conn.Close(); //下面的代码是真正删除此条数据 com.CommandText="delete from Users where usename='"+name+"'"; int i = 0; try { conn.Open(); i = com.ExecuteNonQuery(); } catch { Console.WriteLine("连接服务器失败!!"); } finally { conn.Close(); } if (i > 0)//判断是否删除成功 { Console.WriteLine("删除成功!"); com.CommandText = "select *from Users";//查询所有用户信息 conn.Open(); SqlDataReader dr1 = com.ExecuteReader(); if (dr1.HasRows)//判断是否有用户信息 { while (dr1.Read())//判断是否有下一行,若有直接打印 { Console.WriteLine(dr1["code"] + " " + dr1["usename"] + " " + dr1["password"] + " " + dr1["nick"] + " " + (Convert.ToBoolean(dr1["sex"]) ? "男" : "女") + " " + dr1["nation"] + " " + dr1["class"] + " " + (Convert.ToDateTime(dr1["birthday"]).ToShortDateString())); } } else//无用户信息 { Console.WriteLine("无数据!!"); } conn.Close(); } else { Console.WriteLine("删除失败!"); } } else//用户选择不删除 { Console.WriteLine("删除操作取消!!"); } } else//未查到用户信息 { Console.WriteLine("用户名输入错误!!"); } conn.Close(); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace update { class Program { static void Main(string[] args) { bool has = false;//设置一中间变量判断用输入的数据是否存在,true表示存在,false不存在 Console.Write("请输入要修改的用户名:"); string name = Console.ReadLine(); SqlConnection conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123"); SqlCommand com = conn.CreateCommand(); com.CommandText = "select *from Users where usename='"+name+"'"; conn.Open(); SqlDataReader dr = com.ExecuteReader(); if (dr.HasRows)//如果查到此用户信息 { has = true;//将中间变量更改为true,证明有此条信息 if(dr.Read())//输出修改前要修改的用户的信息 { Console.WriteLine(dr["code"]+" "+dr["usename"]+" "+dr["password"]+" "+dr["nick"]+" "+(Convert.ToBoolean(dr["sex"])?"男":"女")+" "+dr["nation"]+" "+dr["class"]+" "+(Convert.ToDateTime(dr["birthday"]).ToShortDateString())); } } conn.Close(); if (has) { Console.Write("已查到此用户信息,是否要修改?(Y/N)"); string mm = Console.ReadLine(); //记录用户的操作 if (mm.ToUpper() == "Y")//判断用户操作,如果是Y,说明要修改 { Console.Write("请输入要修改的密码:"); string pwd = Console.ReadLine(); Console.Write("请输入要修改的昵称:"); string nick = Console.ReadLine(); Console.Write("请输入要修改的性别:"); string sex = Console.ReadLine(); Console.Write("请输入要修改的生日:"); string bir = Console.ReadLine(); Console.Write("请输入要修改的民族:"); string nation = Console.ReadLine(); Console.Write("请输入要修改的班级:"); string cla = Console.ReadLine(); //防攻击 com.CommandText = "update Users set PASSWORD=@pwd,nick=@nick,sex=@sex,nation=@nation,class=@cla,birthday=@bir where usename=@name"; com.Parameters.Clear(); com.Parameters.AddWithValue("@pwd", pwd); com.Parameters.AddWithValue("@nick", nick); com.Parameters.AddWithValue("@sex", sex); com.Parameters.AddWithValue("@nation", nation); com.Parameters.AddWithValue("@cla", cla); com.Parameters.AddWithValue("@name", name); com.Parameters.AddWithValue("@bir", bir); int i = 0; try { conn.Open(); i = com.ExecuteNonQuery(); } catch(Exception ex) { Console.WriteLine("未连接到服务器! "+ex); } finally { conn.Close(); } if (i > 0) { Console.WriteLine("修改成功!"); com.CommandText = "select code,usename,password,nick,sex,(select nationname from Nation where nationcode=Users.nation) as 民族,(select classname from Class where classcode=Users.class) as 班级,birthday from Users where usename='" + name + "'"; conn.Open(); SqlDataReader dr1 = com.ExecuteReader(); if(dr1.HasRows) { while (dr1.Read())//输出修改后用户的信息 { Console.WriteLine(dr1["code"] + " " + dr1["usename"] + " " + dr1["password"] + " " + dr1["nick"] + " " + (Convert.ToBoolean(dr1["sex"]) ? "男" : "女") + " " + dr1["民族"] + " " + dr1["班级"] + " " + (Convert.ToDateTime(dr1["birthday"]).ToShortDateString())); } } conn.Close(); } else { Console.WriteLine("修改失败!!"); } } else//用户选择不修改 { Console.WriteLine("取消修改!"); } } else//未查到用户信息,判断has=false { Console.WriteLine("无此用户!!"); } Console.ReadLine(); } } }