• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#ReflectionPropertyInfo

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Reflection;


namespace PropertyInfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

          
        }

    

        protected object AssembleObject(Type type, System.Data.IDataReader reader)
        {
            object o = Activator.CreateInstance(type);

            if (o == null)
                return null;

           System.Reflection.PropertyInfo[] properties = type.GetProperties();//声明一个获取类型属性的变量

            for (int i = 0; i <reader.FieldCount; i++)
            {
                System.Reflection.PropertyInfo property = type.GetProperty(reader.GetName(i));//得到第i列的所有属性
                object val = reader[i];//第i列

                if (val != DBNull.Value && property != null && property.CanWrite)//判断对象是否为空,属性是否为空,属性是否可写!如果都为true
                    property.SetValue(o, reader[i], null);//o对象,reader[i]对象的新值,索引器空的
            }

            return o;
        }

 


        public class User
        {
            private int _Id;

            public int Id
            {
                get { return _Id; }
                set { _Id = value; }
            }
            private string _Name;

            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
            private int _Sex;

            public int Sex
            {
                get { return _Sex; }
                set { _Sex = value; }
            }
            private string _SId;

            public string SId
            {
                get { return _SId; }
                set { _SId = value; }
            }
            private string _Pwd;

            public string Pwd
            {
                get { return _Pwd; }
                set { _Pwd = value; }
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
         
           
            DataTable dt = new DataTable();

            using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db2.mdb;Persist Security Info=True"))
            {
                string sql = @"select * from User";
                conn.Open();

                OleDbCommand cmd = new OleDbCommand("select * from db2.User", conn);

                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "dt");

                dt = ds.Tables[0];

                OleDbDataReader dr = cmd.ExecuteReader();

                User u=new User();
                while (dr.Read())
                {
                    u = (User)AssembleObject(typeof(User), dr);
                }

            }
        }

 


    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
聊聊C++中的几种智能指针(上)发布时间:2022-07-18
下一篇:
win8下在microsoftvisualstudio2012利用ODP.NET连接ORACLE12c发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap