ADO.Net练习1
Posted bosamvs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ADO.Net练习1相关的知识,希望对你有一定的参考价值。
一、
1、Car表数据查出显示
2、请输入要查的汽车名称:
请输入要查的汽车油耗:
请输入要查的汽车马力:
static void Main(string[] args) { SqlConnection Car = new SqlConnection("server=.;database=bosamvs;user=sa;pwd=123456;"); #region 1、Car表数据查出显示 SqlCommand cmd = Car.CreateCommand(); cmd.CommandText = "select code,name,oil,powers,exhaust,price from car"; Car.Open(); SqlDataReader table = cmd.ExecuteReader(); if (table.HasRows) { while (table.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}", table["code"], table["name"], table["oil"], table["powers"], table["exhaust"], table["price"]); } } Car.Close(); #endregion Console.ReadLine(); Console.Clear(); #region 2、请输入要查的汽车名称,油耗,马力: while (true) { Console.Write("请输入查询的汽车名称:"); string name = Console.ReadLine(); Console.Write("请输入查询的汽车油耗:"); string oil = Console.ReadLine(); Console.Write("请输入查询的汽车马力:"); string powers = Console.ReadLine(); #region 输入全部为空 if (name == "" && oil == "" && powers == "") { Console.WriteLine("***********************全部信息***********************"); SqlCommand cmd0 = Car.CreateCommand(); cmd0.CommandText = "select*from car"; try { Car.Open(); SqlDataReader table0 = cmd0.ExecuteReader(); if (table0.HasRows) { while (table0.Read()) { Console.WriteLine("ID:{0}\\t编号:{1}\\t名字:{2}\\t系列:{3}\\t出厂日期:{4}\\t油耗:{5}\\t马力:{6}\\t排量:{7}\\t价格:{8}", table0["ids"], table0["code"], table0["name"], table0["brand"], table0["time"], table0["oil"], table0["powers"], table0["exhaust"], table0["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion else { #region 输入车名不为空 if (name != "") { #region 输入油耗为空 if (oil == "" && powers != "") { int powers1 = Convert.ToInt32(powers); SqlCommand cmd1 = Car.CreateCommand(); cmd1.CommandText = "select code,name,oil,powers,exhaust,price from car where name like \'%" + name + "%\' or powers = " + powers1; try { Car.Open(); SqlDataReader table1 = cmd1.ExecuteReader(); if (table1.HasRows) { while (table1.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table1["code"], table1["name"], table1["oil"], table1["powers"], table1["exhaust"], table1["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 输入马力为空 else if (oil != "" && powers == "") { decimal oil1 = Convert.ToDecimal(oil); SqlCommand cmd2 = Car.CreateCommand(); cmd2.CommandText = "select code,name,oil,powers,exhaust,price from car where name like \'%" + name + "%\' or oil = " + oil1; try { Car.Open(); SqlDataReader table2 = cmd2.ExecuteReader(); if (table2.HasRows) { while (table2.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table2["code"], table2["name"], table2["oil"], table2["powers"], table2["exhaust"], table2["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 输入油耗和马力都为空 else if (oil == "" && powers == "") { SqlCommand cmd3 = Car.CreateCommand(); cmd3.CommandText = "select code,name,oil,powers,exhaust,price from car where name like \'%" + name + "%\'"; try { Car.Open(); SqlDataReader table3 = cmd3.ExecuteReader(); if (table3.HasRows) { while (table3.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table3["code"], table3["name"], table3["oil"], table3["powers"], table3["exhaust"], table3["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 全部输入 else if (oil != "" && powers != "") { decimal oil1 = Convert.ToDecimal(oil); int powers1 = Convert.ToInt32(powers); SqlCommand cmd4 = Car.CreateCommand(); cmd4.CommandText = "select code,name,oil,powers,exhaust,price from car where name like \'%" + name + "%\' or oil = " + oil1 + " or powers = " + powers1; try { Car.Open(); SqlDataReader table4 = cmd4.ExecuteReader(); if (table4.HasRows) { while (table4.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table4["code"], table4["name"], table4["oil"], table4["powers"], table4["exhaust"], table4["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion } #endregion #region 输入油耗不为空 else if (oil != "") { #region 输入车名为空 if (name == "" && powers != "") { decimal oil2 = Convert.ToDecimal(oil); int powers2 = Convert.ToInt32(powers); SqlCommand cmd5 = Car.CreateCommand(); cmd5.CommandText = "select code,name,oil,powers,exhaust,price from car where oil=" + oil2 + " or powers=" + powers2; try { Car.Open(); SqlDataReader table5 = cmd5.ExecuteReader(); if (table5.HasRows) { while (table5.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table5["code"], table5["name"], table5["oil"], table5["powers"], table5["exhaust"], table5["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 输入马力为空 else if (name != "" && powers == "") { decimal oil2 = Convert.ToDecimal(oil); SqlCommand cmd6 = Car.CreateCommand(); cmd6.CommandText = "select code,name,oil,powers,exhaust,price from car where name like \'%" + name + "%\' or oil=" + oil2; try { Car.Open(); SqlDataReader table6 = cmd6.ExecuteReader(); if (table6.HasRows) { while (table6.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table6["code"], table6["name"], table6["oil"], table6["powers"], table6["exhaust"], table6["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 输入车名和马力都为空 else if (name == "" && powers == "") { decimal oil2 = Convert.ToDecimal(oil); SqlCommand cmd7 = Car.CreateCommand(); cmd7.CommandText = "select code,name,oil,powers,exhaust,price from car where oil=" + oil2; Car.Open(); SqlDataReader table7 = cmd7.ExecuteReader(); if (table7.HasRows) { while (table7.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table7["code"], table7["name"], table7["oil"], table7["powers"], table7["exhaust"], table7["price"]); } } Car.Close(); } #endregion } #endregion #region 输入马力不为空 else if (powers != "") { #region 输入车名为空 if (name == "" && oil != "") { decimal oil2 = Convert.ToDecimal(oil); int powers2 = Convert.ToInt32(powers); SqlCommand cmd8 = Car.CreateCommand(); cmd8.CommandText = "select code,name,oil,powers,exhaust,price from car where oil=" + oil2 + " or powers=" + powers2; try { Car.Open(); SqlDataReader table8 = cmd8.ExecuteReader(); if (table8.HasRows) { while (table8.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table8["code"], table8["name"], table8["oil"], table8["powers"], table8["exhaust"], table8["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 输入油耗为空 else if (name != "" && oil == "") { int powers2 = Convert.ToInt32(powers); SqlCommand cmd9 = Car.CreateCommand(); cmd9.CommandText = "select code,name,oil,powers,exhaust,price from car where name like \'%" + name + "%\' or powers=" + powers2; try { Car.Open(); SqlDataReader table9 = cmd9.ExecuteReader(); if (table9.HasRows) { while (table9.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table9["code"], table9["name"], table9["oil"], table9["powers"], table9["exhaust"], table9["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion #region 输入车名和油耗都为空 else if (name == "" && oil == "") { int powers2 = Convert.ToInt32(powers); SqlCommand cmd10 = Car.CreateCommand(); cmd10.CommandText = "select code,name,oil,powers,exhaust,price from car where powers=" + powers2; try { Car.Open(); SqlDataReader table10 = cmd10.ExecuteReader(); if (table10.HasRows) { while (table10.Read()) { Console.WriteLine("编号:{0}\\t名字:{1}\\t油耗:{2}\\t马力:{3}\\t排量:{4}\\t价格:{5}\\t", table10["code"], table10["name"], table10["oil"], table10["powers"], table10["exhaust"], table10["price"]); } } Car.Close(); } catch { Console.WriteLine("查询出错!"); } } #endregion } #endregion } } #endregion Console.ReadLine();
以上是关于ADO.Net练习1的主要内容,如果未能解决你的问题,请参考以下文章