ado属性扩展

Posted 爱意红沉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ado属性扩展相关的知识,希望对你有一定的参考价值。

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;
            }
        }
    }
}

 

以上是关于ado属性扩展的主要内容,如果未能解决你的问题,请参考以下文章

2017-04-21Ado.Nte属性扩展

ADO数据库访问类查询属性扩展

ADO SQL属性扩展————多表组合成新的更完整的表

ADO.NET - Size 属性的大小为 0 无效

查看发票组代码后的总结和有感

从recordset.field.type 属性中获取ADO 数据类型的名称?