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的更多相关文章
- ADO.NET对象的详解
1. Connection 类 和数据库交互,必须连接它.连接帮助指明数据库服务器.数据库名字.用户名.密码,和连接数据库所需要的其它参数.Connection对象会被Command对象使用,这样就能 ...
- WebForm获取GET或者POST参数到实体的转换,ADO.NET数据集自动转换实体
最近在修改维护以前的webform项目(维护别人开发的.....)整个aspx没有用到任何的控件,这个我也比较喜欢不用控件所以在提交信息的时候需要自己手动的去Request.QueryString[] ...
- ADO.NET编程之美----数据访问方式(面向连接与面向无连接)
最近,在学习ADO.NET时,其中提到了数据访问方式:面向连接与面向无连接.于是,百度了一下,发现并没有很好的资料,然而,在学校图书馆中发现一本好书(<ASP.NET MVC5 网站开发之美&g ...
- ADO.NET一小记-select top 参数问题
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 最近使用ADO.NET的时候,发现select top @count xxxx 不 ...
- .NET基础拾遗(6)ADO.NET与数据库开发基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...
- 升讯威ADO.NET增强组件(源码):送给喜欢原生ADO.NET的你
目前我们所接触到的许多项目开发,大多数都应用了 ORM 技术来实现与数据库的交互,ORM 虽然有诸多好处,但是在实际工作中,特别是在大型项目开发中,容易发现 ORM 存在一些缺点,在复杂场景下,反而容 ...
- ADO.NET Entity Framework 在哪些场景下使用?
在知乎回答了下,顺手转回来. Enity Framework已经是.NET下最主要的ORM了.而ORM从一个Mapping的概念开始,到现在已经得到了一定的升华,特别是EF等对ORM框架面向对象能力的 ...
- ADO.NET 核心对象简介
ADO.NET是.NET中一组用于和数据源进行交互的面向对象类库,提供了数据访问的高层接口. ADO.NOT类库在System.Data命名空间内,根据我们访问的不同数据库选择命名空间,System. ...
- ODBC、OLE DB、 ADO的区别
转自:http://blog.csdn.net/yinjingjing198808/article/details/7665577 一.ODBC ODBC的由来 1992年Microsoft和Syba ...
- LINQ to SQL语句(19)之ADO.NET与LINQ to SQL
它基于由 ADO.NET 提供程序模型提供的服务.因此,我们可以将 LINQ to SQL 代码与现有的 ADO.Net 应用程序混合在一起,将当前 ADO.NET 解决方案迁移到 LINQ to S ...
随机推荐
- resultMap 表示转换字段后 resultType 表示没·有转换字段
resultMap 表示转换字段后 resultType 表示没·有转换字段
- [Swerc2014 C]Golf Bot
题意:给你N个数字,每次利用这N个数字中最多两个数字进行加法运算,来得到目标中的M个数字. Solution: 我们先来看看多项式乘法:\(A(x)=\sum_{i=0}^{n-1}a_ix^i\), ...
- LOJ #2145. 「SHOI2017」分手是祝愿
题目链接 LOJ #2145 题解 一道画风正常的--期望DP? 首先考虑如何以最小步数熄灭所有灯:贪心地从大到小枚举灯,如果它亮着则修改它.可以求出总的最小步数,设为\(cnt\). 然后开始期望D ...
- 洛谷P4774 BZOJ5418 LOJ2721 [NOI2018]屠龙勇士(扩展中国剩余定理)
题目链接: 洛谷 BZOJ LOJ 题目大意:这么长的题面,就饶了我吧emmm 这题第一眼看上去没法列出同余方程组.为什么?好像不知道用哪把剑杀哪条龙…… 仔细一看,要按顺序杀龙,所以获得的剑出现的顺 ...
- 错误日志收集sentry的安装与简单使用
通过官方文档https://docs.sentry.io/可以得知,安装服务有两种方式,一种是使用Python,这种方式个人感觉比较麻烦.于是选择了第二种方式:使用docker. 我是在Windows ...
- Sparrow.Chart.Wpf控件的动态调用
最近需要在Wpf程序中显示曲线,感觉Sparrow.Chart.Wpf控件不错(http://sparrowtoolkit.codeplex.com/),完全开源的一个控件支持,可以通过nuget下载 ...
- elementUI 通用确认框
Util.vue <script> import VueResource from 'vue-resource' function confirm(_this, operate, fun) ...
- [QuickRoR]Ruby on Rails开发环境安装
1.Setup Ruby on Rails2.Test Web App3.Create the First Web App 1.Setup Ruby on Rails1) Download rubyi ...
- asp.net分页之AJAX 分页
查询功能是开发中最重要的一个功能,大量数据的显示,我们用的最多的就是分页. 在ASP.NET 中有很多数据展现的控件,比如Repeater.GridView,用的最多的GridView,它同时也自带了 ...
- CSUST 1506 ZZ的计算器 模拟题
题目描述:实现一个计算器,可以进行任意步的整数以内的加减乘除运算,运算符号只有+.-.*./,求出结果. 解题报告:一个可以说麻烦的模拟题,我们可以这样,输入以字符串的形式输入,然后将输入先做一遍预处 ...