예제 #1
0
파일: CPQuery.cs 프로젝트: maojw/MyMis
        /// <summary>
        /// Froms the specified parameterized SQL.
        /// </summary>
        /// <param name="parameterizedSQL">The parameterized SQL.</param>
        /// <param name="argsObject">The arguments object.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">parameterizedSQL</exception>
        /// <exception cref="System.InvalidOperationException">不支持在IN条件中使用DateTime类型</exception>
        public static CPQuery From(string parameterizedSQL, object argsObject)
        {
            if (string.IsNullOrEmpty(parameterizedSQL))
            {
                throw new ArgumentNullException("parameterizedSQL");
            }
            CPQuery query = new CPQuery(parameterizedSQL);

            if (argsObject != null)
            {
                foreach (PropertyInfo info in argsObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    object obj2          = info.FastGetValue(argsObject);
                    string parameterName = "@" + info.Name;
                    if ((obj2 == null) || (obj2 == DBNull.Value))
                    {
                        query._command.Parameters.AddWithValue(parameterName, DBNull.Value);
                    }
                    else if (obj2 is ICollection)
                    {
                        StringBuilder builder = new StringBuilder(0x80);
                        builder.Append("(");
                        bool flag = true;
                        foreach (object obj3 in obj2 as ICollection)
                        {
                            string str2 = null;
                            if ((obj3 is string) || (obj3 is Guid))
                            {
                                str2 = "N'" + obj3.ToString().Replace("'", "''") + "'";
                            }
                            else
                            {
                                if (obj3 is DateTime)
                                {
                                    throw new InvalidOperationException("不支持在IN条件中使用DateTime类型");
                                }
                                if (obj3 is Guid)
                                {
                                    str2 = "'" + obj3.ToString() + "'";
                                }
                                else
                                {
                                    str2 = obj3.ToString();
                                }
                            }
                            if (flag)
                            {
                                builder.Append(str2);
                                flag = false;
                            }
                            else
                            {
                                builder.AppendFormat(",{0}", str2);
                            }
                        }
                        if (builder.Length == 1)
                        {
                            builder.Append("NULL");
                        }
                        builder.Append(")");
                        string newValue = builder.ToString();
                        query._sb.Replace(parameterName, newValue);
                    }
                    else
                    {
                        SqlParameter parameter = obj2 as SqlParameter;
                        if (parameter != null)
                        {
                            query._command.Parameters.Add(parameter);
                        }
                        else
                        {
                            query._command.Parameters.AddWithValue(parameterName, obj2);
                        }
                    }
                }
            }
            return(query);
        }