支持多类型数据库的c#数据库模型示例


当前第2页 返回上一页

namespace DynamicFramework
{
    public sealed class DbHelper
    {
        public static List<T> ToList<T>(System.Data.IDataReader reader) 
        {
            List<T> list = new List<T>();
            Csla.Data.SafeDataReader sr = new Csla.Data.SafeDataReader(reader);
            while (sr.Read())
            {
                T t = Activator.CreateInstance<T>();
                Type entityType = t.GetType();
                for (int i = 0; i < sr.FieldCount; i++)
                {
                    string pName = reader.GetName(i);
                    System.Reflection.PropertyInfo p = entityType.GetProperty(pName);
                    if (p != null)
                    {
                        p.SetValue(t, GetValue(p,sr,i), null);
                    }
                }
                list.Add(t);
            }           
            return list;
        }

        private static object GetValue(System.Reflection.PropertyInfo p,Csla.Data.SafeDataReader sr,int index)
        {
            if (p.PropertyType == typeof(string))
            {
                return sr.GetString(index);
            }
            else if (p.PropertyType == typeof(int))
            {
                return sr.GetInt32(index);
            }
            else if (p.PropertyType == typeof(decimal))
            {
                return sr.GetDecimal(index);
            }
            else if (p.PropertyType == typeof(DateTime))
            {
                return sr.GetDateTime(index);
            }
            else if (p.PropertyType == typeof(bool))
            {
                return sr.GetBoolean(index);
            }
            else if (p.PropertyType == typeof(double))
            {
                return sr.GetDouble(index);
            }
            else
            {
                return sr.GetValue(index);
            }

        }

        public static T ToEntity<T>(System.Data.IDataReader reader)
        {
            Csla.Data.SafeDataReader sr = new Csla.Data.SafeDataReader(reader);
            while (sr.Read())
            {
                T t = Activator.CreateInstance<T>();
                Type entityType = t.GetType();
                for (int i = 0; i < sr.FieldCount; i++)
                {
                    string pName = reader.GetName(i);
                    System.Reflection.PropertyInfo p = entityType.GetProperty(pName);
                    if (p != null)
                    {
                        p.SetValue(t, GetValue(p, sr, i), null);
                    }
                }
                return t;
            }
            return default(T);
        }

        public static List<T> TableToList<T>(System.Data.DataTable dt) 
        {
            return ToList<T>(dt.CreateDataReader());
        }

        public static System.Data.DataTable ListToTable<T>(IList<T> list)
        {
            if (list == null) return null;

            System.Data.DataTable dt = new System.Data.DataTable(typeof(T).Name);

            System.Reflection.PropertyInfo[] props = typeof(T).GetProperties();
            if (props.Length >= 0)
            {
                for (int column = 0; column < props.Length; column++)
                {
                    dt.Columns.Add(props[column].Name, props[column].PropertyType);
                }
            }
            foreach (T item in list)
            {
                System.Data.DataRow dr = dt.NewRow();
                foreach (System.Data.DataColumn column in dt.Columns)
                {
                    dr[column] = item.GetType().GetProperty(column.ColumnName).GetValue(item, null);
                }
                dt.Rows.Add(dr);
            }
            //dt.AcceptChanges();           
            return dt;
        }

        public static System.Data.DataTable ToTable(System.Data.IDataReader reader)
        {
            System.Data.DataTable dt = new System.Data.DataTable();          
            dt.Load(reader);
            return dt;
        }

        public static void SaveEntity<T>(T obj)
        {
            string tb = obj.GetType().Name;
            string SQL = "insert into {0}({1})values({2})";
            string fles = "";
            string sparam = "";
            Dictionary<string, object> dicParams = new Dictionary<string, object>();
            foreach (System.Reflection.PropertyInfo var in obj.GetType().GetProperties())
            {
                fles += var.Name + ",";
                sparam += "@" + var.Name + ",";
                dicParams.Add("@" + var.Name,var.GetValue(obj, null));
            }
            SQL = string.Format(SQL, tb, fles.Remove(fles.Length - 1), sparam.Remove(sparam.Length - 1));
            DataAccess.ServerDb.ExecuteScalar(SQL, dicParams);           
        }

        public static void SaveLocalEntity<T>(T obj)
        {
            string tb = obj.GetType().Name;
            string SQL = "insert into {0}({1})values({2})";
            string fles = "";
            string sparam = "";
            Dictionary<string, object> dicParams = new Dictionary<string, object>();
            foreach (System.Reflection.PropertyInfo var in obj.GetType().GetProperties())
            {
                fles += var.Name + ",";
                sparam += "@" + var.Name + ",";
                dicParams.Add("@" + var.Name, var.GetValue(obj, null));
            }
            SQL = string.Format(SQL, tb, fles.Remove(fles.Length - 1), sparam.Remove(sparam.Length - 1));
            DataAccess.LocalDb.ExecuteScalar(SQL, dicParams);
        }
    }


    #region DataAsss == OleDb - SqlClient - SQLite

    public class OleAccess : DataAccess
    {
        public OleAccess()
        {
            connection = new System.Data.OleDb.OleDbConnection();
        }

        public OleAccess(string connectionString)
        {
            connection = new System.Data.OleDb.OleDbConnection(connectionString);
            cnnstr = connectionString;
        }

        internal override System.Data.Common.DbDataAdapter CreateAdapter()
        {
            return new System.Data.OleDb.OleDbDataAdapter();
        }

        public override System.Data.Common.DbCommandBuilder CreateCommandBuilder()
        {
            return new System.Data.OleDb.OleDbCommandBuilder();
        }
    }

    public class SqlClientAccess : DataAccess
    {
        public SqlClientAccess()
        {
            connection = new System.Data.SqlClient.SqlConnection();
        }

        public SqlClientAccess(string connectionString)
        {
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            cnnstr = connectionString;
        }

        internal override System.Data.Common.DbDataAdapter CreateAdapter()
        {
            return new System.Data.SqlClient.SqlDataAdapter();
        }

        public override System.Data.Common.DbCommandBuilder CreateCommandBuilder()
        {
            return new System.Data.SqlClient.SqlCommandBuilder();
        }
    }

    public class SQLiteAccess : DataAccess
    {
        public SQLiteAccess()
        {
            connection = new System.Data.SQLite.SQLiteConnection();
        }

        public SQLiteAccess(string connectionString)
        {
            connection = new System.Data.SQLite.SQLiteConnection(connectionString);
            cnnstr = connectionString;
        }

        internal override System.Data.Common.DbDataAdapter CreateAdapter()
        {
            return new System.Data.SQLite.SQLiteDataAdapter();
        }

        public override System.Data.Common.DbCommandBuilder CreateCommandBuilder()
        {
            return new System.Data.SQLite.SQLiteCommandBuilder();
        }
    }


    #endregion
}
 

 


标签:SQLite

返回前面的内容

相关阅读 >>

Sqlite expert pro5.0如何安装可视化数据库管理软件激活教程

Sqlite数据库管理相关命令的使用介绍

windows平台python连接Sqlite3数据库的方法分析

python实现e-mail收集插件实例教程

android Sqlite命令详解(基本命令)

android开发实现的导出数据库到excel表格功能【附源码下载】

cc++qt数据库与sqltablemodel组件应用教程

nodejs中安装ghost出错的原因及解决方法

详解Sqlite中的数据类型

database.net强大的数据库查询管理工具使用图文教程

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...