一个ADO的小练习
Posted 马MZJ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个ADO的小练习相关的知识,希望对你有一定的参考价值。
练习题:
1、Car表数据查出显示
2、请输入要查的汽车名称:输入可以为空。
请输入要查的汽车油耗:输入可以为空。
请输入要查的汽车马力:输入可以为空。
1 SqlConnection a = new SqlConnection("server=.;database=ADOlianxi0425;user=sa;pwd=123;"); 2 SqlCommand b = a.CreateCommand(); 3 string MC = ""; 4 string YH = ""; 5 decimal YH1; 6 string ML = ""; 7 int ML1; 8 for (; ; ) 9 { 10 for (; ; ) 11 { 12 for (; ; ) 13 { 14 Console.Write("请输入汽车名称:"); 15 MC = Console.ReadLine(); 16 if (MC == "") 17 { 18 break; 19 } 20 b.CommandText = "select * from car where name like @MC"; 21 b.Parameters.Clear(); 22 b.Parameters.Add("@MC", "%" + MC + "%"); 23 a.Open(); 24 SqlDataReader c = b.ExecuteReader(); 25 if (c.HasRows) 26 { 27 a.Close(); 28 break; 29 } 30 else 31 { 32 a.Close(); 33 Console.WriteLine("没有这种汽车!请重新输入!"); 34 } 35 36 } 37 for (; ; ) 38 { 39 Console.Write("请输入汽车油耗:"); 40 YH = Console.ReadLine(); 41 if (YH == "") 42 { 43 break; 44 } 45 try 46 { 47 YH1 = Convert.ToDecimal(YH); 48 b.CommandText = "select * from car where oil = @YH"; 49 b.Parameters.Clear(); 50 b.Parameters.Add("@YH", YH1); 51 a.Open(); 52 SqlDataReader c = b.ExecuteReader(); 53 if (c.HasRows) 54 { 55 a.Close(); 56 break; 57 } 58 else 59 { 60 a.Close(); 61 Console.WriteLine("没有这种油耗!请重新输入!"); 62 } 63 } 64 catch 65 { 66 Console.WriteLine("输入错误请重新输入!"); 67 } 68 69 } 70 for (; ; ) 71 { 72 Console.Write("请输入汽车马力:"); 73 ML = Console.ReadLine(); 74 if (ML == "") 75 { 76 break; 77 } 78 try 79 { 80 ML1 = Convert.ToInt32(ML); 81 b.CommandText = "select * from car where powers = @ML"; 82 b.Parameters.Clear(); 83 b.Parameters.Add("@ML", ML1); 84 a.Open(); 85 SqlDataReader c = b.ExecuteReader(); 86 if (c.HasRows) 87 { 88 a.Close(); 89 break; 90 } 91 else 92 { 93 a.Close(); 94 Console.WriteLine("没有这种马力!请重新输入!"); 95 } 96 } 97 catch 98 { 99 Console.WriteLine("输入错误请重新输入!"); 100 } 101 102 } 103 104 if (MC != "") 105 { 106 if (YH != "") 107 { 108 if (ML != "") 109 { 110 b.CommandText = "select * from car where name like @MC1 and oil = @YH1 and powers = @ML1"; 111 break; 112 } 113 else 114 { 115 b.CommandText = "select * from car where name like @MC1 and oil = @YH1"; 116 break; 117 } 118 } 119 else 120 { 121 if (ML != "") 122 { 123 b.CommandText = "select * from car where name like @MC1 and powers = @ML1"; 124 break; 125 } 126 else 127 { 128 b.CommandText = "select * from car where name like @MC1"; 129 break; 130 } 131 } 132 } 133 else 134 { 135 if (YH != "") 136 { 137 if (ML != "") 138 { 139 b.CommandText = "select * from car where oil = @YH1 and powers = @ML1"; 140 break; 141 } 142 else 143 { 144 b.CommandText = "select * from car where oil = @YH1"; 145 break; 146 } 147 } 148 else 149 { 150 if (ML != "") 151 { 152 b.CommandText = "select * from car where powers = @ML1"; 153 break; 154 } 155 else 156 { 157 Console.WriteLine("输入有误请重新输入!"); 158 } 159 } 160 } 161 } 162 if (MC != "") 163 { 164 b.Parameters.Clear(); 165 b.Parameters.Add("@MC1", "%" + MC + "%"); 166 } 167 if (YH != "") 168 { 169 YH1 = Convert.ToDecimal(YH); 170 b.Parameters.Add("@YH1", YH1); 171 } 172 if (ML != "") 173 { 174 ML1 = Convert.ToInt32(ML); 175 b.Parameters.Add("@ML1", ML1); 176 } 177 a.Open(); 178 SqlDataReader c1 = b.ExecuteReader(); 179 if (c1.HasRows) 180 { 181 while (c1.Read()) 182 { 183 Console.WriteLine("车名:" + c1["name"] + " 油耗:" + c1["oil"] + " 马力:" + c1["powers"] + " 价格:" + c1["price"] ); 184 } 185 } 186 else 187 { 188 Console.WriteLine("没有符合条件的车!"); 189 } 190 a.Close(); 191 Console.Write("是否结束查询:"); 192 string HAHAHA = Console.ReadLine(); 193 if(HAHAHA == "是") 194 { 195 Console.Clear(); 196 Console.Write("-----------------------------------谢谢使用------------------------------------"); 197 break; 198 } 199 Console.Clear(); 200 }
以上是关于一个ADO的小练习的主要内容,如果未能解决你的问题,请参考以下文章