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.SqlClient;
7
8 namespace _06_22
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14
15 //练习题:
16
17 #region 显示全部
18 //1、Car表数据查出显示
19 SqlConnection coon = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;");
20 SqlCommand com = coon.CreateCommand();
21
22 com.CommandText = "select * from car";
23 coon.Open();
24 SqlDataReader a1 = com.ExecuteReader();
25 if (a1.HasRows)
26 {
27 while (a1.Read())
28 {
29 Console.WriteLine("编号:" + a1["Code"] + " 品牌:" + a1["name"] + " 油耗:" + a1["oil"] + " 马力:" + a1["powers"] + " 排量:" + a1["exhaust"] + " 价格:" + a1["price"]);
30 }
31 }
32
33 coon.Close();
34 #endregion
35
36 //2、请输入要查的汽车名称:
37 // 请输入要查的汽车油耗:
38 // 请输入要查的汽车马力:
39 //名称:宝马
40 //油耗:8
41 //马力:1
42
43
44
45 for (; ; )
46 {
47 Console.Write("请输入要查的汽车名称:");
48 string cname = Console.ReadLine();
49 Console.Write("请输入要查的汽车油耗:");
50 string coil = Console.ReadLine();
51 Console.Write(" 请输入要查的汽车马力:");
52 string cpowers = Console.ReadLine(); //输入查询内容
53
54
55 com.CommandText = "select * from car where name like @cname and oil like @coil and powers like @cpowers ";
56
57 com.Parameters.Clear();
58 com.Parameters.Add("@cname", "%" + cname + "%");
59 com.Parameters.Add("@coil", "%" + coil + "%");
60 com.Parameters.Add("@cpowers", "%" + cpowers + "%");
61
62 coon.Open();
63 SqlDataReader c1 = com.ExecuteReader();
64 if (c1.HasRows)
65 {
66 while (c1.Read())
67 {
68 Console.WriteLine("编号:" + c1["Code"] + " 品牌:" + c1["name"] + " 油耗:" + c1["oil"] + " 马力:" + c1["powers"] + " 排量:" + c1["exhaust"] + " 价格:" + c1["price"]);
69 }
70 Console.Write("是否结束?[y/n]");
71 string js = Console.ReadLine();
72 if (js == "y")
73 { break; }
74 }
75 else
76 { Console.WriteLine("查无此项!!!重新输入!!"); }
77 coon.Close();
78
79
80
81
82
83 }
84 Console.ReadLine();
85 }
86 }
87 }