ADO.NET删除和修改
2021-06-29 09:05
标签:region hit net data selectall str color amp 操作 //类 SqlConnection conn; public UsersData() public List ulist.Add(u); public bool SelectAll(string uc) cmd.CommandText = "select *from Users where Ucode = @a"; public bool UpdateUser(Users u) cmd.CommandText = "update Users set PassWord = @a, NickName=@b,Sex=@c ,Birthday=@e where Ucode = @d"; int count = cmd.ExecuteNonQuery(); conn.Close(); public List int count = cmd.ExecuteNonQuery(); if (count > 0) } } Console.BackgroundColor = ConsoleColor.White; while (true) if (u == "1") while (true) if (uuu.PassWord.Trim().Length > 4 && uuu.PassWord == PassWord2) break; while (true) while (true) while (true) while (true) UsersData udd = new UsersData(); Console.ReadKey(); //修改 else if (u == "2") if (h) #region 修改密码 #region 修改昵称 Console.Write("请输入正确的日期格式:"); } bool b = new UsersData().UpdateUser(up); } Console.ReadKey(); ADO.NET删除和修改 标签:region hit net data selectall str color amp 操作 原文地址:http://www.cnblogs.com/yujiamin123/p/7141413.html
SqlCommand cmd;
{
conn = new SqlConnection("server=.;database=Data0504;user=sa;pwd=123;");
cmd = conn.CreateCommand();
}
{
List
cmd.CommandText = "select *from Users";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Users u = new Users();
u.Ids = dr["Ids"].ToString();
u.Ucode = dr["Ucode"].ToString();
u.UserName = dr["UserName"].ToString();
u.PassWord = dr["PassWord"].ToString();
u.NickName = dr["NickName"].ToString();
u.Sex = Convert.ToBoolean(dr["Sex"]);
u.Birthday = Convert.ToDateTime(dr["Birthday"]);
u.Nation = dr["Nation"].ToString();
}
conn.Close();
return ulist;
}
{
bool has = false;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", uc);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
has = true;
}
conn.Close();
return has;
}
{
bool ok = false;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", u.PassWord);
cmd.Parameters.AddWithValue("@b", u.NickName);
cmd.Parameters.AddWithValue("@c", u.Sex);
cmd.Parameters.AddWithValue("@d", u.Ucode);
cmd.Parameters.AddWithValue("@e", u.Birthday );
conn.Open();
if (count > 0)
{
ok = true;
}
return ok;
}
List
Users a= new Users();
conn.Open();
cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f,@g)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", a.Ucode );
cmd.Parameters.AddWithValue("@b",a.UserName );
cmd.Parameters.AddWithValue("@c",a.PassWord );
cmd.Parameters.AddWithValue("@d",a.NickName );
cmd.Parameters.AddWithValue("@e",a.Sex );
cmd.Parameters.AddWithValue("@f", a.Birthday );
cmd.Parameters.AddWithValue("@g", a.Nation );
list.Add(a);
conn.Close();
{
Console.WriteLine("添加成功");
else
{
Console.WriteLine("失败!");
}
return list;
Console.ForegroundColor = ConsoleColor.Black;
while (true)
{
Console.Clear();
new Test1().DataLook();
{
Console.WriteLine();
Console.WriteLine();
Console.Write("请输入你要执行的操作(添加:1,修改:2,删除:3):");
string u = Console.ReadLine();
{
Users uuu = new Users();
uuu.Ucode = new UsersData().NewUcode();
Console.WriteLine("编号为:" + uuu.Ucode);
while (true)
{
Console.Write("请输入用户名:");
uuu.UserName = Console.ReadLine();
if (uuu.UserName.Trim().Length > 0) { break; }
else { Console.WriteLine("用户名不符合规定!"); }
}
{
Console.Write("请输入密码:");
uuu.PassWord = Console.ReadLine();
Console.Write("请再次输入新的密码:");
string PassWord2 = Console.ReadLine();
{
}
else
{ Console.WriteLine("密码不符合规定!");
}
}
{
Console.Write("请输入昵称:");
uuu.NickName = Console.ReadLine();
if (uuu.NickName.Trim().Length > 0) { break; }
else { Console.WriteLine("昵称不符合规定!"); }
}
{
Console.Write("请输入性别(男/女):");
string ssex = Console.ReadLine();
if (ssex != "男" && ssex != "女")
{
Console.WriteLine("性别输入有误!");
}
else
{
uuu.Sex = ssex == "男" ? true : false;
break;
}
}
{
Console.Write("请输入生日(2000-1-1):");
string bir = Console.ReadLine();
try
{
uuu.Birthday = Convert.ToDateTime(bir);
break;
}
catch
{
Console.WriteLine("日期输入错误!");
}
}
{
Console.Write("请输入你的民族(如:汉族/藏族):");
string nat = Console.ReadLine();
string snn = new UserNationData().SelectNationCode(nat);
if (snn == "null")
{
Console.WriteLine("查无此民族!");
}
else
{
uuu.Nation = snn;
break;
}
}
bool ok = udd.InsertUser(uuu);
if (ok)
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
break;
}
{
Console.Write("请输入要修改的用户编号:");
string iii = Console.ReadLine();
bool h = new UsersData().SelectAll(iii);
{
Users up = new Users();
up.Ucode = iii;
Console.WriteLine("已查询到此用户信息,可以进行修改");
while (true)
{
Console.Write("请输入新的密码:");
string pwd1 = Console.ReadLine();
Console.Write("请再次输入新的密码:");
string pwd2 = Console.ReadLine();
if (pwd1 == pwd2)
{
up.PassWord = pwd1;
break;
}
else
{
Console.WriteLine("两次密码不一致!请重新输入!");
}
}
#endregion
while (true)
{
Console.Write("请输入新的昵称:");
string nn = Console.ReadLine();
if (nn.Trim().Length {
Console.WriteLine("输入的昵称不符合规定!请重新输入!");
}
else
{
up.NickName = nn;
break;
}
}
#endregion
while (true)
{
Console.Write("请输入新的性别(男/女):");
string ss = Console.ReadLine();
if (ss != "男" && ss != "女")
{
Console.WriteLine("输入错误!");
}
else
{
up.Sex = ss == "男" ? true : false;
break;
}
}
while (true) {
try
{
Console.Write("请输入新的生日:");
DateTime birthday = new DateTime();
birthday = Convert.ToDateTime(Console.ReadLine());
up.Birthday = birthday;
break;
}
catch {
}
while (true)
{
Console.Write("请输入你的民族(如:汉族/藏族):");
string nat = Console.ReadLine();
string snn = new UserNationData().SelectNationCode(nat);
if (snn == "null")
{
Console.WriteLine("查无此民族!");
}
else
{
up.Nation = snn;
break;
}
}
if (b)
{
Console.WriteLine("修改成功!");
}
else
{
Console.WriteLine("修改失败!!");
}
else
{
Console.WriteLine("无此数据");
}
break;
}
下一篇:ADO.NET删除和修改