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 "";
                }




            }


  • 相关阅读:
    面向对象(静态,抽象,接口)--2017-04-20
    面向对象三大特性(面试经常问)--2017-04-18
    析构函数,函数重载,以及面向对象求面积的例子--2017-04-19
    密码强弱的判断(用正则表达式写)---2017-04-17
    php面向对象(一)---2017-04-17
    php数组--2017-04-16
    正则表达式 详解---2017-04-16
    JavaScript BOM 遗漏知识再整理;弹窗和记时事件;
    JavaScript HTML DOM---遗漏知识再整理(向html添加/删除元素,改变内容和css)
    bootstrap部分---网格系统;(几天没写博客了,为了潜心研究一下bootstrap)
  • 原文地址:https://www.cnblogs.com/change4now/p/9176690.html
Copyright © 2011-2022 走看看