using System; using System.Collections.Generic; using System.Linq; using System.Text; using 属性扩展.Model; using 属性扩展.DataOperation; namespace 属性扩展 { class Program { static void Main(string[] args) { List<Score> list = new ScoreData().SelectAll(); foreach (Score s in list) { Console.WriteLine(s.Sno + " " + s.Sname + " " + s.Ssex + " " + s.Cno + " " + s.CnoName + " " + s.Degree); } Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using 属性扩展.Model; using System.Data.SqlClient; namespace 属性扩展.DataOperation { public class ScoreData { SqlConnection conn = null; SqlCommand cmd = null; public ScoreData() { conn = new SqlConnection("server=.;database=Data05152;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public List<Score> SelectAll() { List<Score> list = new List<Score>(); cmd.CommandText = "select *from score"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Score s = new Score(); s.Cno = dr["cno"].ToString(); s.Sno = Convert.ToInt32(dr["sno"]); s.Degree = Convert.ToDecimal(dr["degree"]); list.Add(s); } } conn.Close(); return list; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace 属性扩展.Model { public class Score { private int _Sno; public int Sno { get { return _Sno; } set { _Sno = value; } } private string _Cno; public string Cno { get { return _Cno; } set { _Cno = value; } } public string CnoName { get { string n = ""; SqlConnection conn = new SqlConnection("server=.;database=Data05152;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from course where cno = @cno"; cmd.Parameters.Clear(); cmd.Parameters.Add("@cno", _Cno); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { n = dr["cname"].ToString(); } } conn.Close(); return n; } } private decimal _Degree; public decimal Degree { get { return _Degree; } set { _Degree = value; } } public string Sname { get { string n = ""; SqlConnection conn = new SqlConnection("server=.;database=Data05152;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from student where sno = @sno"; cmd.Parameters.Clear(); cmd.Parameters.Add("@sno", _Sno); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { n = dr["sname"].ToString(); } } conn.Close(); return n; } } public string Ssex { get { string n = ""; SqlConnection conn = new SqlConnection("server=.;database=Data05152;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from student where sno = @sno"; cmd.Parameters.Clear(); cmd.Parameters.Add("@sno", _Sno); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { n = dr["ssex"].ToString(); } } conn.Close(); return n; } } } }
时间: 2024-10-31 18:13:15