zoukankan      html  css  js  c++  java
  • 数据访问----实例1

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Data;
     7 using System.Data.SqlClient;
     8 
     9 namespace yy
    10 {
    11     class Program
    12     {
    13         public static void Login()
    14         {
    15             Console.Write("请输入账号:");
    16             string name = Console.ReadLine();
    17             Console.Write("请输入密码:");
    18             string password = Console.ReadLine();
    19             SqlConnection lcon = new SqlConnection("server=.;database=xx;uid=sa;pwd=111111");
    20             lcon.Open();
    21 
    22             SqlCommand lcmd = lcon.CreateCommand();
    23             lcmd.CommandText = "select * from Login where Uid='"+name+"' and Pwd='"+password+"'";
    24             SqlDataReader re=lcmd.ExecuteReader();
    25             if (re.HasRows){
    26                 Console.Write("登录成功!"); }
    27             else {
    28                 Console.Write("账号或密码输入不正确!"); 
    29             }
    30 
    31             lcon.Close();
    32 
    33         }
    34         public static void _Stu()
    35         {
    36             Console.WriteLine("**************************操作记录***************************");
    37             Console.WriteLine("请选择:0:增加记录 1:修改记录 2:删除记录 3:读取所有记录 4:读取记录");
    38             string xz = Console.ReadLine();
    39             switch (xz)
    40             {
    41                 case "0": yy.Class1.create();
    42                     break;
    43                 case "1": yy.Class1.updateBysid();
    44                     break;
    45                 case "2": yy.Class1.deleteBysid();
    46                     break;
    47                 case "3": yy.Class1.Read();
    48                     break;
    49                 case "4": yy.Class1.Cha();
    50                     break;
    51                 default:
    52                     Console.WriteLine("输入有误!!");
    53                     break;
    54             }
    55 
    56         }
    57       
    58         static void Main(string[] args)
    59         {
    60 
    61            // Login();
    62 
    63             _Stu();
    64            
    65             
    66             Main(args);
    67 
    68         }
    69 
    70     }
    71 }
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 using System.Data;
      7 using System.Data.SqlClient;
      8 namespace yy
      9 {
     10     class Class1
     11     {
     12         public static void create()
     13         {
     14             Console.WriteLine("**************************增加记录***************************");
     15             Console.Write("学号:");
     16             string sid = Console.ReadLine();
     17             Console.Write("姓名:");
     18             string sname = Console.ReadLine();
     19             Console.Write("性别:");
     20             string ssex = Console.ReadLine();
     21             Console.Write("班级:");
     22             string sclass = Console.ReadLine();
     23 
     24             SqlConnection scon = new SqlConnection("server=.;database=xx;uid=sa;pwd=111111");
     25             scon.Open();
     26             SqlCommand scmd = new SqlCommand();
     27             scmd.Connection = scon;
     28             scmd.CommandText = "insert into _Stu values('" + sid + "','" + sname + "','" + ssex + "','" + sclass + "')";
     29 
     30             scmd.ExecuteNonQuery();
     31             Console.WriteLine("OK!");
     32 
     33             scon.Close();
     34 
     35 
     36         }
     37 
     38         public static void updateBysid()
     39         {
     40             Console.WriteLine("**************************修改记录***************************");
     41             Console.Write("学号:");
     42             string id = Console.ReadLine();
     43             Console.Write("姓名:");
     44             string name = Console.ReadLine();
     45             Console.Write("性别:");
     46             string sex = Console.ReadLine();
     47             Console.Write("班级:");
     48             string iclass = Console.ReadLine();
     49 
     50             SqlConnection scon = new SqlConnection("server=.;database=xx;uid=sa;pwd=111111");
     51             scon.Open();
     52             SqlCommand scmd = new SqlCommand();
     53             scmd.Connection = scon;
     54             scmd.CommandText = "update _Stu set sname='"+name
     55                 +"',ssex='"+sex+"',sclass='"+iclass+"' where     sid='"+id+"'";
     56 
     57             scmd.ExecuteNonQuery();
     58             Console.WriteLine("OK!");
     59             scon.Close();
     60 
     61 
     62         }
     63         public static void Read()
     64         {
     65             Console.WriteLine("**************************读取所有记录***************************");
     66             SqlConnection rcon = new SqlConnection("server=.;database=xx;uid=sa;pwd=111111");
     67             rcon.Open();
     68             SqlCommand rcmd = rcon.CreateCommand();
     69             rcmd.CommandText = "select * from _Stu";
     70             SqlDataReader re = rcmd.ExecuteReader();
     71             while (re.Read())
     72             {
     73                 Console.Write(re[0].ToString()+"	"+re[1].ToString()+"	"+re[2].ToString()+"	"+re[3].ToString()+"
    ");
     74             }
     75 
     76             rcon.Close();
     77 
     78         }
     79         public static void Cha()
     80         {
     81             Console.WriteLine("**************************查询记录***************************");
     82             Console.WriteLine("请输入查询条件:0:按学号 1:按姓名 2:按班级 3:按性别 ");
     83             string xz = Console.ReadLine();
     84             string tj = "";
     85             SqlConnection rcon = new SqlConnection("server=.;database=xx;uid=sa;pwd=111111");
     86             rcon.Open();
     87             SqlCommand rcmd = rcon.CreateCommand();
     88 
     89             switch (xz)
     90             {
     91                 case "0":
     92                     Console.Write("请输入需要查询的学号:");
     93                     tj = Console.ReadLine();
     94                     rcmd.CommandText = "select * from _Stu where sid='"+tj+"'";
     95                     break;
     96                 case "1":
     97                     Console.Write("请输入需要查询的姓名:");
     98                     tj = Console.ReadLine();
     99                     rcmd.CommandText = "select * from _Stu where sname='" + tj + "'";
    100                     break;
    101                 case "2":
    102                     Console.Write("请输入需要查询的班级:");
    103                     tj = Console.ReadLine();
    104                     rcmd.CommandText = "select * from _Stu where sclass='" + tj + "'";
    105                     break;
    106                 case "3":
    107                     Console.Write("请输入需要查询的性别:");
    108                     tj = Console.ReadLine();
    109                     rcmd.CommandText = "select * from _Stu where ssex='" + tj + "'";
    110                     break;
    111                 case "4":
    112                     hh();
    113                     break;
    114                 default:
    115                     Console.Write("别闹!");
    116 
    117                     break;
    118             }
    119             
    120             SqlDataReader re = rcmd.ExecuteReader();
    121             Console.WriteLine("**************************查询结果***************************");
    122             while (re.Read())
    123             {
    124                 Console.Write(re[0].ToString() + "	" + re[1].ToString() + "	" + re[2].ToString() + "	" + re[3].ToString() + "
    ");
    125             }
    126 
    127             rcon.Close();
    128 
    129         }
    130         public static void hh()
    131         { 
    132         
    133         }
    134         public static void deleteBysid()
    135         {
    136             Console.WriteLine("**************************删除记录***************************");
    137             Console.Write("请选择:0:学号 1:姓名 2:班级 3:性别");
    138             string xz = Console.ReadLine();
    139             SqlConnection scon=new SqlConnection("server=.;database=xx;uid=sa;pwd=111111");
    140             scon.Open();
    141             SqlCommand scmd=new SqlCommand();
    142             scmd.Connection=scon;
    143             string code="  ";
    144             switch (xz)
    145             {
    146                 case "0":
    147                     Console.Write("学号:");
    148             string id = Console.ReadLine();
    149             code = "delete from _Stu  where sid='" + id + "'";
    150                     break;
    151                 case "1":
    152                     Console.Write("姓名:");
    153                     string name = Console.ReadLine();
    154                     code = "delete from _Stu  where sname='" + name + "'";
    155 
    156                     break;
    157 
    158                 case "2":
    159                     Console.Write("班级:");
    160                     string iclass = Console.ReadLine();
    161                     code = "delete from _Stu  where scalss='" + iclass + "'";
    162 
    163                     break;
    164 
    165                 case "3":
    166                     Console.Write("性别:");
    167                     string sex = Console.ReadLine();
    168                     code = "delete from _Stu  where ssex='" + sex+ "'";
    169                     break;
    170 
    171                 default:
    172                     Console.WriteLine("有错误!");
    173                     break;
    174             }
    175             scmd.CommandText = code;
    176             scmd.ExecuteNonQuery();
    177             Console.WriteLine("OK!");
    178             scon.Close();
    179             
    180 
    181             
    182         }
    183 
    184     }
    185 }
  • 相关阅读:
    hdu 4864 Task
    hdu 1501 Zipper
    hdu 1428 漫步校园
    hdu 1505 City Game
    hdu 1337 The Drunk Jailer
    9-13记录
    python 读取unicode编码文件
    梯度出现Nan值的追踪
    Rstudio-server更改R版本
    stdout/stderr作用学习
  • 原文地址:https://www.cnblogs.com/lovesy2413/p/4475974.html
Copyright © 2011-2022 走看看