zoukankan      html  css  js  c++  java
  • Parameter 参数替换成value

    该方法来到hubbledotnet源代码方法

    路径:hubble.Data-->hubble.SQLClient-->HubbleCommand类里面的BuildSqlWithSqlParameter方法

    在此谢谢eaglet

    View Code
     #region static methods
    
            private static string BuildSqlWithSqlParameter(string sql,System.Data.Common.DbParameter[] paras)
            {
                System.Data.Common.DbParameter[] arrParas = new System.Data.Common.DbParameter[paras.Length];
    
                for (int i = 0; i < paras.Length; i++)
                {
                    arrParas[i] = paras[i];
                }
    
                Array.Sort(arrParas, new DbParameterForSort());
    
                object[] objParas = new object[arrParas.Length];
    
                for (int i = 0; i < arrParas.Length; i++)
                {
                    sql = sql.Replace(arrParas[i].ParameterName, "{" + i.ToString() + "}");
    
                    switch (arrParas[i].DbType)
                    {
                        case System.Data.DbType.AnsiString:
                        case System.Data.DbType.AnsiStringFixedLength:
                        case System.Data.DbType.String:
                             if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] =arrParas[i].Value;
                            }
                            break;
                        case System.Data.DbType.StringFixedLength:
                        case System.Data.DbType.Xml:
                            objParas[i] = arrParas[i].Value as string;
                            break;
    
                        case System.Data.DbType.Boolean:
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = arrParas[i].Value.ToString();
                            }
                            break;
    
                        case System.Data.DbType.Date:
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = ((DateTime)arrParas[i].Value).ToString("yyyy-MM-dd");
                            }
                            break;
    
                        case System.Data.DbType.DateTime:
                        case System.Data.DbType.DateTime2:
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = Convert.ToDateTime(arrParas[i].Value);// ((DateTime)arrParas[i].Value).ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            break;
                        case System.Data.DbType.Time:
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = ((DateTime)arrParas[i].Value).ToString("HH:mm:ss");
                            }
                            break;
                        case System.Data.DbType.Byte:
                        case System.Data.DbType.UInt16:
                        case System.Data.DbType.UInt32:
                        case System.Data.DbType.UInt64:
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = ulong.Parse(arrParas[i].Value.ToString());
                            }
                            break;
    
                        case System.Data.DbType.Decimal:
                        case System.Data.DbType.Double:
                        case System.Data.DbType.Single:
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = double.Parse(arrParas[i].Value.ToString());
                            }
                            break;
    
                        case System.Data.DbType.Int16:
                        case System.Data.DbType.Int32:
                        case System.Data.DbType.Int64:
                        case System.Data.DbType.SByte:
    
                            if (arrParas[i].Value == null)
                            {
                                objParas[i] = null;
                            }
                            else
                            {
                                objParas[i] = long.Parse(arrParas[i].Value.ToString());
                            }
                            break; 
    
                        default:
                            throw new System.Data.DataException(string.Format("Invalid parameter DataType: {0}", arrParas[i].DbType));
                    }
                }
    
                return BuildSql(sql, objParas);
            }
    
            class DbParameterForSort : IComparer<System.Data.Common.DbParameter>
            {
    
                #region IComparer<DbParameter> Members
    
                public int Compare(System.Data.Common.DbParameter x, System.Data.Common.DbParameter y)
                {
                    return y.ParameterName.CompareTo(x.ParameterName);
                }
    
                #endregion
            }
    
    
    
            public static string BuildSql(string sql, object[] paras)
            {
                if (paras == null)
                {
                    return sql;
                }
                else
                {
                    object[] parameters = new object[paras.Length];
                    paras.CopyTo(parameters, 0);
    
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        object obj = parameters[i];
    
                        if (obj == null)
                        {
                            parameters[i] = "NULL";
                        }
                        else if (obj is string)
                        {
                            parameters[i] = string.Format("'{0}'",
                                ((string)obj).Replace("'", "''"));
                        }
                        else if (obj is bool)
                        {
                            parameters[i] = string.Format("'{0}'", obj);
                        }
                        else if (obj is DateTime)
                        {
                            parameters[i] = string.Format("'{0}'",
                                ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                        else if (obj is byte[])
                        {
                            StringBuilder sb = new StringBuilder();
    
                            sb.Append("0x");
    
                            foreach (byte b in (byte[])obj)
                            {
                                sb.AppendFormat("{0:x1}", b);
                            }
    
                            parameters[i] = sb.ToString();
                        }
                        else
                        {
                            parameters[i] = obj.ToString();
                        }
    
                    }
    
                    return string.Format(sql, parameters);
                }
            }
    
            #endregion
  • 相关阅读:
    前端常用js库地址
    react简介
    es6 decorator
    es6 之 class
    es6之对象扩展
    vs code 默认浏览器
    不同环境查看是否可访问外网、访问网址、ip地址、外网ip地址
    httprunner2简单了解,httprunner可实现的功能,jmeter均可实现,所以,未深入学习
    结构化问答
    《大数的认识》
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/2975099.html
Copyright © 2011-2022 走看看