利用反射讀取資料庫資料

林堯彬發表於2020-04-04
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    public class DBHelper
    {
        private static string ConnectionStringCustomers = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;




        public void Query()
        { }

        public T QueryDomain<T>()
        {
            int OrderID = 2;
            Type type = typeof(T);

            T t = (T)Activator.CreateInstance(type);
            foreach( var a in type.GetProperties())
            {
                Console.WriteLine("11");
            }
            string column = string.Join(",", type.GetProperties().Select(p => string.Format("{0}", p.Name)));


            string sql = string.Format( "select {0} from[{1}] where OrderID={2}",column,type.Name,OrderID);
            using (SqlConnection coon = new SqlConnection(ConnectionStringCustomers))

            {
                SqlCommand sqlcommand = new SqlCommand(sql, coon);
                
                coon.Open();
                SqlDataReader reder=sqlcommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (reder.Read())
                {
                    foreach (var a in type.GetProperties())
                    {
                        string aName = a.Name;
                                               
                        a.SetValue(t, reder[aName]);

                        Console.WriteLine("shuxing{0},zhi:{1}", a.Name,a.GetValue(t));
                    }
                }
            }
            return default(T);
            }


    }
 
   
}

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="connectionstring" connectionString="Data Source=.;Initial Catalog=books;User ID=sa;Password=123123;Integrated Security=True;Pooling=False" />
  </connectionStrings>
</configuration>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
           
           Type type=typeof(Class1);//尋找物件

            object oobject=Activator.CreateInstance(type);//尋找物件

            foreach (var a in type.GetProperties())
            {
                Console.WriteLine("屬性名稱{0},值是{1}", a.Name, a.GetValue(oobject));
            }
            DBHelper dbhelper = new DBHelper();

            dbhelper.QueryDomain<Class1>();
            Console.ReadLine();
        }
    }
}

 資料庫實體類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Class1
    {
        public int OrderID { get; set; }

        public string Address { get; set; }

    }
}

轉載於:https://www.cnblogs.com/LZXX/p/6706560.html

相關文章