示例#1
0
        private void SetInArrayParameter(string placeholder, ICollection collection)
        {
            StringBuilder sb = new StringBuilder(128);

            // 优先检查 int[], Guid[] 类型,并转成SQL语句中的一部分
            // 因为这些强类型的数据本身是安全的,不存在注入,就不转换成命令参数。
            CPQuery.ArrayToString(collection, sb);

            if (sb.Length == 0)                 // 如果不是 int[], Guid[] ,就转换成命令参数

            {
                foreach (object obj in collection)
                {
                    string name = "x" + (_paramIndex++).ToString();

                    DbParameter parameter = _command.CreateParameter();
                    parameter.ParameterName = ParaNameBuilder.GetParaName(name);
                    parameter.Value         = obj;
                    _command.Parameters.Add(parameter);

                    if (sb.Length > 0)
                    {
                        sb.Append(',');
                    }

                    sb.Append(ParaNameBuilder.GetPlaceholder(name));
                }
            }

            if (sb.Length == 0)
            {
                sb.Append("NULL");
            }

            _command.CommandText = _command.CommandText.Replace(placeholder, sb.ToString());
        }