扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年10月2日
关键字:
在本页阅读全文(共2页)
把常用的sql方法写成一个类,看起来非常清晰,功能简单。大家常用的的SqlHelper类既有存储过程又有sql语句实现的方法,让新手一看就晕(我现在偶尔晕晕)~~
通用函数类(字符串处理等)
namespace IndiaStudyChannel.Utils

{
    /**//// <summary>
    /// Summary description for Utils.
    /// </summary>
    /// 由 liudao 翻译整理
    /// 该源码下载自www.51aspx.com(51aspx.com)
    public class Utils
    
{
        public Utils()
        
{
        }

        /**//// <summary>
        /// This method removes some dangerous characters from the word to avoid 'Sql Injection attack'.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string MakeSafeWord(string s)
        
{
            if ( s == null )
                return "";
            return s.Replace("'", "").Replace("--", "");
        }

        /**//// <summary>
        /// This method checks if the passed user id is an adinistrator or if this is same as current user.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool IsOwner(object userId)
        
{
            if ( System.Web.HttpContext.Current.Session["CurrentUser"] == null )
            
{
                // There is no userid saved in session. This means user has not logged in.
                return false;
            }
            // Get current user from session.
            string currentUser = System.Web.HttpContext.Current.Session["CurrentUser"].ToString();
            // Get the admin user id from config file.
            string adminUser = System.Configuration.ConfigurationSettings.AppSettings["AdminUser"];
            if ( currentUser == adminUser )
            
{
                // Current user is an administrator. Administrator is Owner for all submissions.
                return true;
            }
            if ( userId != null && userId.ToString() == currentUser )
            
{
                // Current user is same as the userId passed.
                return true;
            }
            return false;
        }
    
        /**//// <summary>
        /// This method checks if the passed user id is an adinistrator or if this is same as current user.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool IsAdministrator()
        
{
            if ( System.Web.HttpContext.Current.Session["CurrentUser"] == null )
            
{
                // There is no userid saved in session. This means user has not logged in.
                return false;
            }
            // Get current user from session.
            string currentUser = System.Web.HttpContext.Current.Session["CurrentUser"].ToString();
            // Get the admin user id from config file.
            string adminUser = System.Configuration.ConfigurationSettings.AppSettings["AdminUser"];
            if ( currentUser == adminUser )
            
{
                // Current user is an administrator. Administrator is Owner for all submissions.
                return true;
            }
            return false;
        }
        public static string FormatFileName(string s)
        
{
            char[] chars = 
{'#', '@', '?', ':', '\'', '\"', '.', '/', '\\', ' ', '<', '>', '&', '*', '(', ')', '!', ',', ';', ':', '-', '+', '='};
            foreach (char c in chars)
            
{
                s = s.Replace(c, '-');
            }
            
            s = s.Replace(" ", "-");
            s = s.Replace("--", "-");
            if ( s.LastIndexOf("-") == (s.Length - 1) && s.Length > 2 )
            
{
                s = s.Substring(0, s.Length - 1);
            }
            return s;
        }
    }
}
        protected void Page_Load(object sender, System.EventArgs e)
        
{
            string query = "Select UserId, Name, Email, DateJoined from Members";
            dg.DataSource = Utils.DataManager.ExecuteQuery(query);
            
            dg.DataBind();
        }如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。