create database guoji--建立数据库 go use guoji go create table xinxi--建立表一 ( name varchar(20), minzu varchar(20) ) insert into xinxi values('lisi','101'); insert into xinxi values('wangwu','102'); insert into xinxi values('zhaoliu','103'); select * from xinxi go create table minz--建立表二 ( mcode varchar(20), mname varchar(20) ) insert into minz values('101','汉族'); insert into minz values('102','满族'); insert into minz values('103','藏族'); go select * from minz
要求:查询表一,民族部分用汉族展现出来!!!
//进行xinxi表的实体化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace biao.App_Code { public class xinxi { private string _name; public string name { get { return _name; } set { _name = value; } } private string _minzu; public string minzu//用这个接受数据 { get { return _minzu; } set { _minzu = value; } } public string minzustr//用这个输出数据 { get { minzdata n = new minzdata(); string m = n.mname(_minzu); return m; } }
//进行xinxi表的数据数据访问类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace biao.App_Code { public class xinxidata { SqlConnection cnn = null; SqlCommand cmd = null; public xinxidata() { cnn = new SqlConnection("server=.;database=guoji;user=sa;pwd=123"); cmd = cnn.CreateCommand(); } public List<xinxi> select()//查询全部信息,把信息放到集合里 { xinxi x = null; List<xinxi> list = new List<xinxi>(); cmd.CommandText = "select * from xinxi"; cnn.Open(); SqlDataReader ss = cmd.ExecuteReader(); if (ss.HasRows) { while (ss.Read()) { x = new xinxi(); x.name = ss["name"].ToString(); x.minzu = ss["minzu"].ToString(); list.Add(x); } } else { Console.WriteLine("此表为空,没有数据!"); } cnn.Close(); return list; }
//minz表的实体化 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace biao.App_Code { public class minz { private string _mcode; public string mcode { get { return _mcode; } set { _mcode = value; } } private string _mname; public string mname { get { return _mname; } set { _mname = value; } } } }
//minz表的数据访问类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace biao.App_Code { public class minzdata { SqlConnection cnn = null; SqlCommand cmd = null; public minzdata() { cnn = new SqlConnection("server=.;database=guoji;user=sa;pwd=123"); cmd = cnn.CreateCommand(); } public string mname(string code)//根据编号查姓名 { string a = "<无>"; cmd.CommandText = "select * from minz where mcode=@a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",code); cnn.Open(); SqlDataReader ss = cmd.ExecuteReader(); if (ss.HasRows)//是否有这个数据 { ss.Read(); a = ss[1].ToString(); } else { Console.WriteLine("数据表为空"); } cnn.Close(); return a; } } }
//在program类的主函数 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using biao.App_Code; namespace biao { class Program { static void Main(string[] args) { xinxidata a = new xinxidata(); List<xinxi> list = a.select(); foreach (xinxi b in list)//遍历集合查询全部内容 { Console.WriteLine(b.name + " " + b.minzustr);//用b.minzustr输出语句 } Console.ReadLine(); } } }
完!!