zoukankan      html  css  js  c++  java
  • ADO.NET增删改完整作业

    while (true) {
    Console.Write("请输入您要执行的操作:添加:1,修改:2,删除:3:");
    int a= Convert .ToInt32 ( Console.ReadLine());
    try{
    if (a == 1)
    {
    Console.WriteLine("接下来执行添加程序:");
    string ucode = "";
    SqlConnection conn = new SqlConnection("server=.;database=Data0504;user=sa;pwd=123;");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "select * from Users order by Ucode desc";
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    ucode = dr["Ucode"].ToString();
    int b = Convert.ToInt32(ucode.Substring(1));
    b++;
    ucode = "U" + b.ToString("000");
    conn.Close();

    Console.WriteLine("您的编号是:" + ucode);
    string username = "";
    while (true)
    {
    Console.Write("请输入用户名:");
    string str = Console.ReadLine();
    try
    {
    username = Convert.ToString(str);
    if (username == "")
    {
    Console.Write("名字不能为空,请重新输入:");
    }
    else
    {
    break;
    }

    }
    catch
    {
    break;
    }
    }
    string nickname = "";
    while (true)
    {
    Console.Write("请输入昵称:");
    string str = Console.ReadLine();
    try
    {
    nickname = Convert.ToString(str);
    if (nickname == "")
    {
    Console.Write("名字不能为空,请重新输入:");
    }
    else
    {
    break;
    }

    }
    catch
    {
    break;
    }
    }
    string password = "";
    while (true)
    {
    Console.Write("请输入密码:");
    string str = Console.ReadLine();
    try
    {
    password = Convert.ToString(str);
    if (password.Length >= 4)
    {
    break;
    }
    else
    {
    Console.Write("请重新输入至少四位的密码:");
    }

    }
    catch
    {
    break;
    }
    }
    string sex = "";
    while (true)
    {
    Console.Write("请输入性别:");

    sex = Console.ReadLine();
    try
    {

    if (sex=="男")
    {
    Convert.ToBoolean(sex="True");
    break;
    }
    else if (sex == "女")
    {
    Convert.ToBoolean(sex = "False");
    }
    else
    {
    Console.Write("请输入男女:");
    }

    }
    catch
    {
    break;
    }
    }
    DateTime birthday = new DateTime();
    while (true)
    {

    try
    {
    Console.Write("请输入生日:");
    birthday = Convert.ToDateTime(Console.ReadLine());

    break;

    }
    catch
    {
    Console.Write("请输入正确的日期格式:");
    }
    }

    Console.Write("请输入民族:");
    string nation = Console.ReadLine();
    conn.Open();
    cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f,@g)";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@a", ucode);
    cmd.Parameters.AddWithValue("@b", username);
    cmd.Parameters.AddWithValue("@c", password);
    cmd.Parameters.AddWithValue("@d", nickname);
    cmd.Parameters.AddWithValue("@e",sex);
    cmd.Parameters.AddWithValue("@f", birthday);
    cmd.Parameters.AddWithValue("@g", nation);
    int count = cmd.ExecuteNonQuery();
    conn.Close();
    if (count > 0)
    {
    Console.WriteLine("添加成功");

    }
    else
    {
    Console.WriteLine("失败!");
    }
    }
    else if (a == 2)
    {
    Console.WriteLine("执行修改程序");
    bool 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();
    string jg = "";
    if (dr.HasRows)
    {
    Console.Write("已查到本编号是否修改?N/Y:");
    string str = Console.ReadLine();
    jg = Convert .ToString (str);
    has = true;

    }

    conn.Close();

    if (has&&jg=="Y")
    {

    string username = "";
    while (true)
    {
    Console.Write("请输入用户名:");
    string str = Console.ReadLine();
    try
    {
    username = Convert.ToString(str);
    if (username == "")
    {
    Console.Write("名字不能为空,请重新输入:");
    }
    else
    {
    break;
    }

    }
    catch
    {
    break;
    }
    }
    string nickname = "";
    while (true)
    {
    Console.Write("请输入昵称:");
    string str = Console.ReadLine();
    try
    {
    nickname = Convert.ToString(str);
    if (nickname == "")
    {
    Console.Write("名字不能为空,请重新输入:");
    }
    else
    {
    break;
    }

    }
    catch
    {
    break;
    }
    }
    string password = "";
    while (true)
    {
    Console.Write("请输入密码:");
    string str = Console.ReadLine();
    try
    {
    password = Convert.ToString(str);
    if (password.Length >= 4)
    {
    break;
    }
    else
    {
    Console.Write("请重新输入至少四位的密码:");
    }

    }
    catch
    {
    break;
    }
    }

    string sex = "";
    while (true)
    {
    Console.Write("请输入性别:");

    sex = Console.ReadLine();
    try
    {

    if (sex == "男")
    {
    Convert.ToBoolean(sex = "True");
    break;
    }
    else if (sex == "女")
    {
    Convert.ToBoolean(sex = "False");
    }
    else
    {
    Console.Write("请输入男女:");
    }

    }
    catch
    {
    break;
    }
    }

    DateTime birthday = new DateTime();
    while (true)
    {

    try
    {

    Console.Write("请输入生日:");
    birthday = Convert .ToDateTime (Console.ReadLine());
    break;

    }
    catch
    {
    Console.Write("请输入正确的日期格式:");
    }
    }

    Console.Write("请输入民族:");
    string nation = Console.ReadLine();
    conn.Open();
    cmd.CommandText = "update Users set UserName=@b, PassWord=@c,NickName=@d, Sex=@e, Birthday=@f, Nation=@g where Ucode=@a";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@a", ucode);
    cmd.Parameters.AddWithValue("@b", username);
    cmd.Parameters.AddWithValue("@c", password);
    cmd.Parameters.AddWithValue("@d", nickname);
    cmd.Parameters.AddWithValue("@e", sex);
    cmd.Parameters.AddWithValue("@f", birthday);
    cmd.Parameters.AddWithValue("@g", nation);


    int count = cmd.ExecuteNonQuery();

    conn.Close();

    if (count > 0)
    {
    Console.WriteLine("修改成功!");
    continue;
    }
    else
    {
    Console.WriteLine("修改失败!");
    break;
    }


    }else if(jg=="N"){
    continue;

    }

    else
    {

    Console.WriteLine("未查到此编号数据,请确定编号是否正确!");
    }

    }


    else if (a == 3)
    {
    while (true)
    {
    try
    {
    Console.WriteLine("执行修改程序");
    bool 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();
    string jg = "";
    if (dr.HasRows)
    {

    Console.Write("已查到本编号是否删除?N/Y:");
    string str = Console.ReadLine();
    jg = Convert.ToString(str);
    has = true;
    }

    conn.Close();

    if (has && jg == "Y")
    {
    conn.Open();
    cmd.CommandText = "delete from Users where Ucode=@a ";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@a", ucode);
    conn.Close();
    conn.Open();
    int count = cmd.ExecuteNonQuery();

    conn.Close();

    if (count > 0)
    {
    Console.WriteLine("删除成功!");
    break;
    }
    else
    {
    Console.WriteLine("删除失败!");
    break;
    }

    }
    else if (jg == "N")
    {

    continue;
    }
    else
    {

    Console.WriteLine("未查到此编号数据,请确定编号是否正确!");
    }
    }
    catch
    {

    break;
    }


    }
    }
    }
    catch
    {

    Console.Write("请输入您要执行的操作:添加:1,修改:2,删除:3:");


    }


    }
    Console.ReadLine();

  • 相关阅读:
    指针细说
    C++编码规范101
    关于C标准
    Bjarne Stroustrup语录2
    Bjarne Stroustrup 语录1
    计算机网络基础识别
    ssh
    matplotlib画图
    数据分析之Numpy
    数据分析
  • 原文地址:https://www.cnblogs.com/yujiamin123/p/7128267.html
Copyright © 2011-2022 走看看