namespace 完整修改
{
class Program
{
static void Main(string[] args)
{
bool has = false;
Console.Write("請輸入要修改的用戶名:");
string Uname = Console.ReadLine();
SqlConnection conn = new SqlConnection("server=.;database=Data0720;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select *from Users where UserName='" + Uname + "'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
has = true;
}
conn.Close();
if (has)
{
Console.WriteLine("已經查到[" + Uname + "]用戶信息,請進行修改");
Console.Write("請輸入修改後的密碼:");
string Pwd = Console.ReadLine();
Console.Write("請輸入修改後的昵稱:");
string Nname = Console.ReadLine();
Console.Write("請輸入修改後的性別:");
string Sex = Console.ReadLine();
Console.Write("請輸入修改後的生日:");
string Birthday = Console.ReadLine();
Console.Write("請輸入修改後的民族:");
string Nation = Console.ReadLine();
//@key 占位符
cmd.CommandText = " update Users set PassWord=@pwd,NickName=@nname,Sex=@sex,Birthday=@bir,Nation=@nat where UserName=@uname;";
cmd.Parameters.Clear();
cmd.Parameters.Add("@pwd", Pwd);
cmd.Parameters.Add("@nname",Nname);
cmd.Parameters.Add("@sex", (Sex=="男")?"1":"0");
cmd.Parameters.Add("@bir", Birthday);
cmd.Parameters.Add("@nat", Nation);
cmd.Parameters.Add("@uname", Uname);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("修改完畢!");
}
else
{
Console.WriteLine("未查到[" + Uname + "]用戶信息,請確認用戶名輸入是否正確!");
}
Console.ReadLine();
}
}
}
namespace 完整修改刪除_數據庫字符串攻擊
{
class Program
{
static void Main(string[] args)
{
bool has = false;
Console.Write("請輸入要刪除的用戶名:");
string Uname = Console.ReadLine();
SqlConnection conn = new SqlConnection("server=.;database=Data0720;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select *from Users where UserName='" + Uname + "'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
has = true;
}
conn.Close();
if (has)
{
Console.WriteLine("已經查到[" + Uname + "]用戶信息,是否確定要刪除嗎?(Y/N):");
string u = Console.ReadLine();
if (u.ToUpper() == "Y")//確定刪除
{
cmd.CommandText = "delete from Users where UserName='" + Uname + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine(Uname + "刪除成功!");
}
}
else
{
Console.WriteLine("未查到[" + Uname + "]用戶信息,請確認用戶名輸入是否正確!");
}
Console.ReadLine();
}
}
}