• ADO.NET修改数据


      1             Console.Write("请输入要修改的编号:");
      2             string ucode = Console.ReadLine();
      3             SqlConnection conn = new SqlConnection("server=.;database=Data0504;user=sa;pwd=123;");
      4             SqlCommand cmd = conn.CreateCommand();
      5 
      6             cmd.CommandText = "select *from Users where Ucode=@a";
      7             cmd.Parameters.Clear();
      8             cmd.Parameters.AddWithValue("@a", ucode);
      9 
     10             conn.Open();
     11             SqlDataReader dr = cmd.ExecuteReader();
     12             bool hh=false;
     13 
     14             if (dr.HasRows)
     15             {
     16                 hh = true;
     17             }
     18             conn.Close();
     19 
     20             if (hh)
     21             {
     22 
     23                 string username = "";
     24                 while (true)
     25                 {
     26                     Console.Write("请输入用户名:");
     27                     string str = Console.ReadLine();
     28                     try
     29                     {
     30                         username = Convert.ToString(str);
     31                         if (username == "")
     32                         {
     33                             Console.Write("名字不能为空,请重新输入:");
     34                         }
     35                         else
     36                         {
     37                             break;
     38                         }
     39 
     40                     }
     41                     catch
     42                     {
     43                         break;
     44                     }
     45                 }
     46                 string nickname = "";
     47                 while (true)
     48                 {
     49                     Console.Write("请输入昵称:");
     50                     string str = Console.ReadLine();
     51                     try
     52                     {
     53                         nickname = Convert.ToString(str);
     54                         if (nickname == "")
     55                         {
     56                             Console.Write("名字不能为空,请重新输入:");
     57                         }
     58                         else
     59                         {
     60                             break;
     61                         }
     62 
     63                     }
     64                     catch
     65                     {
     66                         break;
     67                     }
     68                 }
     69                 string password = "";
     70                 while (true)
     71                 {
     72                     Console.Write("请输入密码:");
     73                     string str = Console.ReadLine();
     74                     try
     75                     {
     76                         password = Convert.ToString(str);
     77                         if (password.Length >= 4)
     78                         {
     79                             break;
     80                         }
     81                         else
     82                         {
     83                             Console.Write("请重新输入至少四位的密码:");
     84                         }
     85 
     86                     }
     87                     catch
     88                     {
     89                         break;
     90                     }
     91                 }
     92 
     93                 string sex = "";
     94                 while (true)
     95                 {
     96                     Console.Write("请输入性别:");
     97 
     98                     sex = Console.ReadLine();
     99                     try
    100                     {
    101 
    102                         if (sex == "")
    103                         {
    104                             Convert.ToBoolean(sex = "True");
    105                             break;
    106                         }
    107                         else if (sex == "")
    108                         {
    109                             Convert.ToBoolean(sex = "False");
    110                         }
    111                         else
    112                         {
    113                             Console.Write("请输入男女:");
    114                         }
    115 
    116                     }
    117                     catch
    118                     {
    119                         break;
    120                     }
    121                 }
    122 
    123                 DateTime birthday = new DateTime();
    124                 while (true)
    125                 {
    126 
    127                     try
    128                     {
    129 
    130                         Console.Write("请输入生日:");
    131                         birthday = Convert.ToDateTime(Console.ReadLine());
    132                         break;
    133 
    134                     }
    135                     catch
    136                     {
    137                         Console.Write("请输入正确的日期格式:");
    138                     }
    139                 }
    140 
    141                 Console.Write("请输入民族:");
    142                 string nation = Console.ReadLine();
    143                 conn.Open();
    144                 cmd.CommandText = "update Users set UserName=@b, PassWord=@c,NickName=@d, Sex=@e, Birthday=@f, Nation=@g where Ucode=@a";
    145                 cmd.Parameters.Clear();
    146                 cmd.Parameters.AddWithValue("@a", ucode);
    147                 cmd.Parameters.AddWithValue("@b", username);
    148                 cmd.Parameters.AddWithValue("@c", password);
    149                 cmd.Parameters.AddWithValue("@d", nickname);
    150                 cmd.Parameters.AddWithValue("@e", sex);
    151                 cmd.Parameters.AddWithValue("@f", birthday);
    152                 cmd.Parameters.AddWithValue("@g", nation);
    153 
    154                 int count = cmd.ExecuteNonQuery();
    155                 conn.Close();
    156 
    157                 if (count > 0)
    158                 {
    159                     Console.WriteLine("修改成功!");
    160                 }
    161                 else
    162                 {
    163                     Console.WriteLine("修改失败!");
    164                 }
    165             }
    166             else
    167             {
    168 
    169                 Console.WriteLine("未查到此编号数据,请确定编号是否正确!");
    170             }
  • 相关阅读:
    安装SQL Server驱动到Maven仓库[转]
    Nuget 摘录
    删除除了Src属性以后的全部属性
    执行NET 命令无法使用超过20个字符的组名或用户名
    在EntityFramework中使用 nock的方法。
    两代码的区别
    SQLServer 执行计划
    win7电脑的账户被禁用了怎么办
    win10防火墙损坏如何修复
    win10摄像头在哪打开?
  • 原文地址:https://www.cnblogs.com/wcl2017/p/7136414.html
Copyright © 2020-2023  润新知