标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication14.model { class car { //建立数据模型 //car数据表的各项属性建立成一个类的形式 private string _Code; /// <summary> /// 汽车编号 /// </summary> public string Code { get { return _Code; } set { _Code = value; } } private string _Name; /// <summary> /// 汽车名称 /// </summary> public string Name { get { return _Name; } set { _Name = value; } } private string _Brand; /// <summary> /// 汽车系别 /// </summary> public string Brand { get { return _Brand; } set { _Brand = value; } } private DateTime _Time; /// <summary> /// 上市时间 /// </summary> public DateTime Time { get { return _Time; } set { _Time = value; } } private decimal _Oil; /// <summary> /// 汽车油耗 /// </summary> public decimal Oil { get { return _Oil; } set { _Oil = value; } } private int _Powers; /// <summary> /// 汽车马力 /// </summary> public int Powers { get { return _Powers; } set { _Powers = value; } } private int _Exhaust; /// <summary> /// 汽车排量 /// </summary> public int Exhaust { get { return _Exhaust; } set { _Exhaust = value; } } private decimal _Price; /// <summary> /// 汽车价格 /// </summary> public decimal Price { get { return _Price; } set { _Price = value; } } private string _Pic; /// <summary> /// 汽车对应图片 /// </summary> public string Pic { get { return _Pic; } set { _Pic = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ConsoleApplication14.model; using System.Data.SqlClient; namespace ConsoleApplication14.dataoperation { class cardata { SqlConnection conn = null;//声明数据连接变量,用于cardata类里所有函数 SqlCommand cmd = null;//声明数据命令变量,用于cardata类里所有函数 //创建构造函数,调用该类便立即进行数据连接和数据操作命令的创建 public cardata() { conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;");//创建数据连接 cmd = conn.CreateCommand();//创建数据操作 } //建立一个查询car表格的函数方法 /// <summary> /// 查询car表里的所有数据 /// </summary> /// <returns>泛型集合类型</returns> public List<car> selectall() { //实例化泛型集合,以便于调用里面的属性 List<car> list = new List<car>(); //编写数据操作指令 cmd.CommandText = "select * from car"; //开启数据库 conn.Open(); //执行读取操作命令 SqlDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) { while(dr.Read()) { //初始化car类,为car类开辟一个内存空间,以便于调用car类里的属性 car c = new car(); //为car每个属性里赋值 c.Code = dr["Code"].ToString(); c.Name = dr["Name"].ToString(); c.Brand = dr["Brand"].ToString(); c.Time = Convert.ToDateTime(dr["Time"]); c.Oil = Convert.ToDecimal(dr["Oil"]); c.Powers = Convert.ToInt32(dr["Powers"]); c.Exhaust = Convert.ToInt32(dr["Exhaust"]); c.Price = Convert.ToDecimal(dr["Price"]); c.Pic = dr["Pic"].ToString(); //将car类c里的数值放到泛型集合list内 list.Add(c); } } //关闭数据库 conn.Close(); //返回泛型集合类型的值 return list; } //建立用汽车姓名模糊查询的函数 public List<car> selectname(string name)//该函数有返回值,有参数,返回值是泛型集合类型,参数是string类型 { //定义一个泛型集合类型的返回值 List<car> list=new List<car>(); //编写查询指令语句 cmd.CommandText = "select * from car where Name like @name"; //防注入式字符串攻击 cmd.Parameters.Clear(); cmd.Parameters.Add("@name","%"+name+"%"); //数据库开启 conn.Open(); //执行数据读取操作 SqlDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) { while(dr.Read()) { //初始化一个car类型的变量来接收数据库car表里的每条数据 car c = new car(); //用c里面的每一个属性接收car表里的每一条记录 c.Code = dr["Code"].ToString(); c.Name = dr["Name"].ToString(); c.Brand = dr["Brand"].ToString(); c.Time = Convert.ToDateTime(dr["Time"]); c.Oil = Convert.ToDecimal(dr["Oil"]); c.Powers = Convert.ToInt32(dr["Powers"]); c.Exhaust = Convert.ToInt32(dr["Exhaust"]); c.Price = Convert.ToDecimal(dr["Price"]); c.Pic = dr["Pic"].ToString(); //并将每一条值放入list集合中 list.Add(c); } } //数据库关闭 conn.Close(); //返回list值 return list; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ConsoleApplication14.dataoperation; using ConsoleApplication14.model; namespace ConsoleApplication14 { class Program { static void Main(string[] args) { //建立数据模型和数据访问类 //查询数据库里data0425数据库类car表的所有内容 //引用命名空间 //定义查询函数的返回类型为泛型集合list类, //所有要首先初始化一个变量接收cardata类里面查询函数selectall里面的值 List<car> data = new cardata().selectall(); //打印出data 里面的值需要使用遍历集合 foreach(car c in data)//泛型集合里面的类型是car类型 { Console.WriteLine(c.Code+" "+c.Name+" "+c.Brand+" "+c.Time+" "+c.Oil+" "+c.Powers+" "+c.Exhaust+" "+c.Price+" "+c.Pic); } while(true) { //建立一个汽车名字的模糊查询 Console.Write("请输入汽车姓名:"); string uname = Console.ReadLine(); //调用函数,用已经初始化并赋值的data变量接收,同时用新的值覆盖data变量里原来的值 data = new cardata().selectname(uname); //打印数据 Console.Clear(); foreach (car c in data)//泛型集合里面的类型是car类型 { Console.WriteLine(c.Code + " " + c.Name + " " + c.Brand + " " + c.Time + " " + c.Oil + " " + c.Powers + " " + c.Exhaust + " " + c.Price + " " + c.Pic); } } Console.ReadKey(); } } }
标签:
原文地址:http://www.cnblogs.com/fengsantianya/p/5610605.html