zoukankan      html  css  js  c++  java
  • 根据类生成数据库连接

     /// <summary>
            /// 根据类生成数据库连接
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="model"></param>
            /// <param name="tableName">如果为空  设置为类名</param>
            /// <param name="needUrlEncode"></param>
            /// <returns></returns>
            public static string GetSql<T>(T model, string tableName = "", bool needUrlEncode = false) where T : class
            {
                try
                {
                    if (model == null) return "";
                    if (string.IsNullOrEmpty(tableName)) tableName = typeof(T).Name.ToString();
                    var sql = "INSERT INTO   " + tableName + " ({0}) VALUES  ({1});";
                    var prop = model.GetType().GetProperties();
                    var fields = "";
                    var values = "";
                    foreach (var info in prop)
                    {

                        fields += string.Format("`{0}`,", info.Name);

                        if (info.PropertyType == typeof(int) || info.PropertyType == typeof(double) ||
                            info.PropertyType == typeof(float) || info.PropertyType == typeof(decimal))
                        {
                            values += string.Format("{0},", Convert.ChangeType(info.GetValue(model), info.PropertyType));
                        }
                        else if (info.PropertyType == typeof(DateTime) || info.PropertyType == typeof(DateTime?))
                        {
                            var v = info.GetValue(model);
                            if (v == null)
                            {
                                values += string.Format("'{0}',", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {

                                values += string.Format("'{0}',", (Convert.ToDateTime(v)).ToString("yyyy-MM-dd HH:mm:ss"));
                            }
                        }
                        else
                        {
                            var v = (info.GetValue(model) ?? "").ToString();
                            values += string.Format("'{0}',", needUrlEncode ? HttpUtility.UrlEncode(v) : v);
                        }

                    }

                    return string.Format(sql, fields.TrimEnd(','), values.TrimEnd(','));

                }
                catch (Exception ex)
                {
                    LogHelper.Error("根据类生成sql 异常:" + ex.Message);
                    return "";
                }




            }


  • 相关阅读:
    C#DataGridView的简单使用
    Python实现简单登陆验证(文件操作)
    【hive】centos7下apache-hive-3.1.2-bin的安装测试
    【mysql】centos7下mysql的安装以及基本操作
    【hadoop】细读MapReduce的工作原理
    【hadoop】看懂WordCount例子
    【hadoop】在eclipse上运行WordCount的操作过程
    纪念一下,时隔多年,继delphi上成功运行sql之后
    【Linux下Hadoop-eclipse-plus-3.2.0】编译Hadoop连接eclipse的插件遇见的一系列错误,崩溃的操作
    数据挖掘初次接触!学习代码
  • 原文地址:https://www.cnblogs.com/change4now/p/9176690.html
Copyright © 2011-2022 走看看