首页 > 代码库 > ADO.NET面向对象增添程序
ADO.NET面向对象增添程序
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
while (true)
{
Console.Clear();
new Test1().DataLook();
while (true)
{
Console.WriteLine();
Console.WriteLine();
Console.Write("请输入你要执行的操作(添加:1,修改:2,删除:3):");
string u = Console.ReadLine();
if (u == "1")
{
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("用户名不符合规定!"); }
}
while (true)
{
Console.Write("请输入密码:");
uuu.PassWord = Console.ReadLine();
Console.Write("请再次输入新的密码:");
string PassWord2 = Console.ReadLine();
if (uuu.PassWord.Trim().Length > 4 && uuu.PassWord == PassWord2)
{
break;
}
else
{ Console.WriteLine("密码不符合规定!");
}
}
while (true)
{
Console.Write("请输入昵称:");
uuu.NickName = Console.ReadLine();
if (uuu.NickName.Trim().Length > 0) { break; }
else { Console.WriteLine("昵称不符合规定!"); }
}
while (true)
{
Console.Write("请输入性别(男/女):");
string ssex = Console.ReadLine();
if (ssex != "男" && ssex != "女")
{
Console.WriteLine("性别输入有误!");
}
else
{
uuu.Sex = ssex == "男" ? true : false;
break;
}
}
while (true)
{
Console.Write("请输入生日(2000-1-1):");
string bir = Console.ReadLine();
try
{
uuu.Birthday = Convert.ToDateTime(bir);
break;
}
catch
{
Console.WriteLine("日期输入错误!");
}
}
while (true)
{
Console.Write("请输入你的民族(如:汉族/藏族):");
string nat = Console.ReadLine();
string snn = new UserNationData().SelectNationCode(nat);
if (snn == "null")
{
Console.WriteLine("查无此民族!");
}
else
{
uuu.Nation = snn;
break;
}
}
UsersData udd = new UsersData();
bool ok = udd.InsertUser(uuu);
if (ok)
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
Console.ReadKey();
break;
}
//类
public string NewUcode()
{
string s = "生成失败!";
cmd.CommandText = "select *from Users Order by Ucode desc";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string code = dr["Ucode"].ToString();
int a = Convert.ToInt32(code.Substring(1));
a++;
s = "U" + a.ToString("000");
}
conn.Close();
return s;
}
public bool InsertUser(Users u)
{
bool ok = false;
cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f,@g)";
cmd.Parameters.Clear();
cmd.Parameters.Add("@a", u.Ucode);
cmd.Parameters.Add("@b", u.UserName);
cmd.Parameters.Add("@c", u.PassWord);
cmd.Parameters.Add("@d", u.NickName);
cmd.Parameters.Add("@e", u.Sex);
cmd.Parameters.Add("@f", u.Birthday);
cmd.Parameters.Add("@g", u.Nation);
conn.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
ok = true;
}
conn.Close();
return ok;
}
ADO.NET面向对象增添程序