首页 > 代码库 > ADO SQL属性扩展————多表组合成新的更完整的表
ADO SQL属性扩展————多表组合成新的更完整的表
create database guoji--建立数据库gouse guojigocreate 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 xinxigocreate table minz--建立表二(mcode varchar(20),mname varchar(20))insert into minz values(‘101‘,‘汉族‘);insert into minz values(‘102‘,‘满族‘);insert into minz values(‘103‘,‘藏族‘);goselect * 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(); } }}
完!!
ADO SQL属性扩展————多表组合成新的更完整的表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。