sql server and oracle 不同之处只在于:

1·参数@和:

2·自增和序列

RPROM.cs //(写错愕,应该是RPORM)

RPROM.Insert(p1); //需求:DBS中插入一条数据

约定:
类名要和表名一样
字段名和数据库列名一样
主键的名字必须是Id,必须自动递增,int类型

class RupengROM
    {
        /// 插入对象
        internal static void Insert(Object obj)
        {
            //获得类名
            Type type = obj.GetType();
            string className = type.Name;
            //获得除id的所有列名
            PropertyInfo[] properties = type.GetProperties();
            string[] propertyNames = new string[properties.Length - 1]; //存除id的属性名(列名)
            string[] propertyAtNames = new string[properties.Length - 1]; //存除id的属性+@名
            //获得除id的所有参数
            MySqlParameter[] mysqlParameters = new MySqlParameter[properties.Length - 1]; //存除id的参数名
            int i = 0;
            foreach(PropertyInfo property in properties)
            {
                if(property.Name!="Id")
                {
                    propertyNames[i] = property.Name;
                    propertyAtNames[i] = "@" + property.Name;
                    MySqlParameter mysqlParam = new MySqlParameter();
                    mysqlParam.ParameterName = "@" + property.Name;
                    mysqlParam.Value = property.GetValue(obj); //获得指定对象的属性值
                    mysqlParameters[i] = mysqlParam;
                    i++;
                }
            }
            string propertyNamesStr = string.Join(",", propertyNames);
            string propertyAtNamesStr = string.Join(",", propertyAtNames);
            //拼接sql语句
            //insert into T_Person(Name,Age) values(@Name,@Age)
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into T_").Append(className).Append("(").Append(propertyNamesStr).Append(") values(").Append(propertyAtNamesStr).Append(")");
            int im = MySqlHelper.ExecuteNonQuery(sb.ToString(), mysqlParameters);
            string msg = im > 0 ? "插入成功" : "插入失败";
            Console.WriteLine(msg);
        }

/// 根据id查询对象
        internal static Object SelectById(Type type,int id)
        {
            string className = type.Name;
            //拼接sql语句
            //select * from T_Person where Id=@Id
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from T_").Append(className).Append(" where Id=@Id");
            DataTable dt = MySqlHelper.ExecuteQuery(sb.ToString(), new MySqlParameter() { ParameterName = "@Id", Value = id });
            if(dt.Rows.Count<=0)
            {
                return null;
            }
            else if(dt.Rows.Count>1)
            {
                throw new Exception("数据库发生异常,存在id=" + id + "的重复数据");
            }
            else
            {
                DataRow row = dt.Rows[0];
                Object obj = Activator.CreateInstance(type); //动态创建指定类型的实例
                PropertyInfo[] propertyInfoes = type.GetProperties();
                foreach(PropertyInfo propertyInfo in propertyInfoes)
                {
                    string propertyName = propertyInfo.Name; //属性名就是列名
                    object value = row[propertyName];
                    propertyInfo.SetValue(obj, value); //设置指定对象的属性值
                }
                return obj;
            }
        }

/// 根据Id更新实例
        internal static void UpdateById(Object obj)
        {
            //获得类名
            Type type = obj.GetType();
            string className = type.Name;
            //获得除id的列名变化形式 "Name=@Name"
            PropertyInfo[] propertyInfoes = type.GetProperties();
            string[] propertyChanges = new string[propertyInfoes.Length - 1]; //存除id的属性变化形式
            MySqlParameter[] mysqlParameters = new MySqlParameter[propertyInfoes.Length]; //存sql参数
            int i = 0;
            foreach(PropertyInfo propertyInfo in propertyInfoes)
            {
                string propertyName = propertyInfo.Name;
                if (propertyName != "Id")
                {
                    propertyChanges[i] = propertyName + "=@" + propertyName;
                    i++;
                }
                MySqlParameter mysqlParam = new MySqlParameter();
                mysqlParam.ParameterName = "@" + propertyName;
                mysqlParam.Value = propertyInfo.GetValue(obj);
                mysqlParameters[i] = mysqlParam;
            }
            string propertyChangesStr = string.Join(",", propertyChanges);

//拼接sql语句
            //update T_Person set Name=@Name,Age=@Age where Id=@Id
            StringBuilder sb = new StringBuilder();
            sb.Append("update T_").Append(className).Append(" set ").Append(propertyChangesStr).Append(" where Id=@Id");
            int im = MySqlHelper.ExecuteNonQuery(sb.ToString(), mysqlParameters);
            string msg = im > 0 ? "更新成功" : "更新失败";
            Console.WriteLine(msg);
        }

/// 根据id删除
        internal static void DeleteById(Type type, int id)
        {
            //获得类名
            string className = type.Name;

//拼接sql语句
            //delete from T_Person where Id=@Id
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from T_").Append(className).Append(" where Id=@Id");
            int im = MySqlHelper.ExecuteNonQuery(sb.ToString(), new MySqlParameter() { ParameterName = "@Id", Value = id });
            string msg = im > 0 ? "删除成功" : "删除失败";
            Console.WriteLine(msg);
        }
    }

最新文章

  1. js代码实现下拉菜单
  2. Python用法摘要 BY 四喜三顺
  3. codeforces 719C (复杂模拟-四舍五入-贪心)
  4. mongodb 的安装和使用
  5. JDE开发端安装笔记
  6. 反射小应用之DataTable和List&lt;T&gt;互操作
  7. iphone开发第二个程序
  8. poi对wps excel的支持
  9. try、catch、finally与return
  10. vim note(4)
  11. 请转到http://zhuangyongyao.com
  12. eclipse 常用的一些设置
  13. opengl笔记——旋转,一段代码的理解
  14. 生成扫描二维码下载app的二维码的方法
  15. 如何使用SecureCRT连接ubuntu
  16. tensorflow源代码方式安装
  17. 网络编程(sock)搞定!
  18. attr与prop html与text
  19. Java泛型相关总结(下)
  20. MongoDB学习目录

热门文章

  1. JAVA Excel导入导出
  2. 提高MySQL效率与性能的技巧
  3. 【bzoj1299】[LLH邀请赛]巧克力棒(博弈论思维题)
  4. 在移动端做查看日志信息的js
  5. flask--Wtform
  6. CLR事件与路由事件在XAML代码中应用时的区别
  7. 迭代式返回 IEnumerable&lt;T&gt;
  8. [Alfred]为Baidu Weather Workflow更新图标
  9. ural 1039 树dp
  10. 解决win10 phptoshop #fff纯白不是这样的白 显示器高级的问题