예제 #1
0
        /// <summary>
        /// 获取SQL中查询出来的全部列名
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        protected List <string> GetFields(string sql)
        {
            List <string> list = new List <string>();

            if (!string.IsNullOrEmpty(sql))
            {
                try
                {
                    string temp = CommonValidation.GetFilter(sql);
                    using (SqlConnection connection = new SqlConnection(DbAccessManager.Connectstring))
                    {
                        connection.Open();
                        SqlCommand createCommand = new SqlCommand(temp, connection);
                        using (SqlDataReader sqlReader = createCommand.ExecuteReader())
                        {
                            for (int index = 0; index < sqlReader.FieldCount; index++)
                            {
                                string name      = sqlReader.GetName(index);
                                Type   fieldType = sqlReader.GetFieldType(index);
                                if (!string.IsNullOrEmpty(name))
                                {
                                    list.Add(name.ToLower());
                                }
                            }
                        }
                    }
                }
                catch (SqlException)
                {
                }
            }
            return(list);
        }
예제 #2
0
        protected bool ValidateSql(string sql, out string error)
        {
            error = "";
            string s = CommonValidation.GetFilter(sql);

            try
            {
                CommonValidation.ExecuteSql(s);
                return(true);
            }
            catch (SqlException sqlEx)
            {
                error = sqlEx.Message;
                if (sqlEx.Number == 306)
                {
                    error += " 参考:cast(columnName as varchar(max))";
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return(false);
        }