优秀是一种习惯,不求进步很大,但求天天进步。

像蜗牛一样爬行,坚信总有一天我有属于我的一片天。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

近日网站开发收获(一)

Posted on 2010-11-11 20:00  StartFromZero  阅读(415)  评论(4编辑  收藏  举报

 

近日网站开发收获(

 

1. Response.write();输出错误,

Response.write"<script>alert('您的ip已经被锁定,解除锁定,请与管理员联系')</script>",); 不会弹出对话框。

解决方案:在其后缺少return语句,并且命名空间引用了system.net(这个应该不是问题的所在)

1.1. SQL:select count(*)和select count(1)的区别

 效果:两者的返回结果是一样的。

意义:当count的参数是具体值时如count(1),count('a'),count的参数已没有实际意义了。

范围:在统计范围,count(*)和count(1) 一样,都包括对NULL的统计;
           count(column) 是不包括NULL的统计。

速度:表沒有主键(Primary key),count(1)比count(*)快;
  否则,主键作为count的参数时,count(主键)比count(1)和count(*)都快;
 表只有一个字段,count(*),count(1)和count(主键)速度一样。

2.sql语句查询同一个ip在十秒内提出次数,加以控制。在少于十秒则禁止提问。

逻辑string sql = "SELECT qip FROM question WHERE datediff(ss,(SELECT TOP 1 qtime FROM question WHERE qip='" + iip + "' ORDER BY qid DESC),getdate())>10";

注释:

1.         Datediff()

DateDiff 函数

返回两个日期之间的时间间隔。

DATEDIFF(datepart,startdate,enddate)

例如:

Datediff(day,2010-10-10,2010-10-11)

输出1

Datediffssstartdateenddatess表示秒,则mi表示分钟。

Getdate()sql中获取当期系统时间。

2.另外一点就是在写sql的过程中的一点思路,比如上面的sql的实现过程,可以采用分步法,然后嵌套语句。这样一个问题就给分解拉。

第一步查询ip地址相同的问题的

Select ip from question where qip=’127.0.0.1’

第二步:查询时间间隔大于10s的

 string sql = "SELECT qip FROM question WHERE datediff(ss,(SELECT TOP 1 qtime FROM question WHERE qip='" + iip + "' ORDER BY qid DESC),getdate())>10"

 3. cmd.Parameters.AddWithValue("@ipsame", iip);

最初是用的这样方式传入sql语句中,一直会提示提问超出10s的对话框,后来换成上面的iip问题得以解决。

4.对应response.write后,页面变形的问题解决方案,

引入ClientScriptManager cs = Page.ClientScript;

然后对应的response.write换成     cs.RegisterClientScriptBlock(typeof(object), "bs", "alert('您的ip已经被锁定,解除锁定,请与管理员联系')", true);

这种方法使用页面不会出现变形。

附加一个很使用的类文件,可以直接引用:

namespace Steam.Core.Web

{

    using Steam.Core.Error;

    using System;

    using System.Text;

    using System.Web;

    using System.Web.UI;

 

    /// <summary>

    /// Javascript处理类

    /// </summary>

    public class JavascriptDeal

    {

        public static void Alert(string str)

        {

            Http.Response.Write("<script>alert('" + Encoding(str) + "');</script>");

        }

 

        public static void Alert(ClientScriptManager cs, string str)

        {

            Alert(cs, str, Guid.NewGuid().ToString());

        }

 

        public static void Alert(ClientScriptManager cs, string str, string key)

        {

            cs.RegisterClientScriptBlock(typeof(object), key, "alert('" + Encoding(str) + "');", true);

        }

 

        /// <summary>

        /// 在页面装载完成后弹出警告框,即在</form>标记的上一行

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="str"></param>

        public static void AlertDown(ClientScriptManager cs, string str)

        {

            AlertDown(cs, str, Guid.NewGuid().ToString());

        }

 

        /// <summary>

        /// 在页面装载完成后弹出警告框,即在</form>标记的上一行

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="str"></param>

        /// <param name="key"></param>

        public static void AlertDown(ClientScriptManager cs, string str, string key)

        {

            cs.RegisterStartupScript(typeof(object), key, "alert('" + Encoding(str) + "');", true);

        }

 

        public static void CloseWindow(HttpResponse response)

        {

            response.Write("<script>window.close();</script>");

            response.End();

        }

 

        public static void CloseWindow(ClientScriptManager cs)

        {

            cs.RegisterClientScriptBlock(typeof(object), "closewindow", "window.close();", true);

        }

 

        public static string Encoding(string str)

        {

            return Encoding(str, @"\n");

        }

 

        public static string Encoding(string str, string enterStr)

        {

            StringBuilder builder = new StringBuilder(str);

            builder.Replace("\r\n", enterStr);

            builder.Replace("\r", enterStr);

            builder.Replace("\n", enterStr);

            builder.Replace(@"\", @"\\");

            builder.Replace("\"", "\\\"");

            builder.Replace("'", @"\'");

            return builder.ToString();

        }

 

        /// <summary>

        /// 输出javascript

        /// </summary>

        /// <param name="response"></param>

        /// <param name="str"></param>

        public static void Out(HttpResponse response, string str)

        {

            response.Write("<script language=\"javascript\" type=\"text/javascript\">" + str + "</script>");

        }

 

        /// <summary>

        /// 输出javascript

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="str"></param>

        public static void Out(ClientScriptManager cs, string str)

        {

            Out(cs, str, Guid.NewGuid().ToString());

        }

 

        /// <summary>

        /// 输出javascript

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="str"></param>

        /// <param name="key"></param>

        public static void Out(ClientScriptManager cs, string str, string key)

        {

            cs.RegisterClientScriptBlock(typeof(object), key, str, true);

        }

 

        /// <summary>

        /// 在页面装载完成后输出javascript块,即在</form>标记的上一行

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="str"></param>

        public static void OutDown(ClientScriptManager cs, string str)

        {

            Out(cs, str, Guid.NewGuid().ToString());

        }

 

        /// <summary>

        /// 在页面装载完成后输出javascript块,即在</form>标记的上一行

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="str"></param>

        /// <param name="key"></param>

        public static void OutDown(ClientScriptManager cs, string str, string key)

        {

            cs.RegisterStartupScript(typeof(object), key, str, true);

        }

 

        /// <summary>

        /// 刷新用Open打开的子窗口

        /// </summary>

        /// <param name="response"></param>

        public static void RefreshOpener(HttpResponse response)

        {

            response.Write("<script>window.opener.Refresh();</script>");

        }

 

        /// <summary>

        /// 刷新父窗口

        /// </summary>

        /// <param name="response"></param>

        public static void RefreshParent(HttpResponse response)

        {

           response.Write("<script>window.dialogArguments.Refresh();</script>");

        }

        /// <summary>

        /// 刷新父窗口

        /// </summary>

        /// <param name="cs"></param>

        public static void RefreshParent(ClientScriptManager cs)

        {

            cs.RegisterClientScriptBlock(typeof(object), "refreshparent", "window.dialogArguments.Refresh();", true);

        }

 

        /// <summary>

        /// 刷新本身

        /// </summary>

        /// <param name="url"></param>

        public static void RefreshSelf(string url)

        {

            HttpResponse response = Http.Response;

            response.Write("<script>window.location.href='" + url + "';</script>");

            response.End();

        }

 

        /// <summary>

        /// 设置子窗口的返回值

        /// </summary>

        /// <param name="cs"></param>

        /// <param name="value"></param>

        public static void SetReturnValue(ClientScriptManager cs, string value)

        {

            cs.RegisterClientScriptBlock(typeof(object), "setreturnvalue", "window.returnValue = '" + value + "';", true);

        }

 

        public static void SetReturnValue(ClientScriptManager cs, string value, string property)

        {

            cs.RegisterClientScriptBlock(typeof(object), "setreturnvalue", "window.returnValue." + property + " = '" + value + "';", true);

        }

 

 

        public static void ShowError(Exception e)

        {

            Page handler = HttpContext.Current.Handler as Page;

            if (handler != null)

            {

                ShowError(handler.ClientScript, e);

            }

        }

 

        public static void ShowError(string str)

        {

            Page handler = HttpContext.Current.Handler as Page;

            if (handler != null)

            {

                ShowError(handler.ClientScript, str);

            }

        }

 

        public static void ShowError(ClientScriptManager cs, Exception e)

        {

            StringBuilder builder = new StringBuilder(e.Message);

            StringBuilder builder2 = new StringBuilder(e.Source);

            StringBuilder builder3 = new StringBuilder(e.StackTrace);

            bool flag = false;

            ValidateSoftException exception = null;

            if (e is ValidateSoftException)

            {

                flag = true;

                exception = e as ValidateSoftException;

            }

            for (Exception exception2 = e.InnerException; exception2 != null; exception2 = exception2.InnerException)

            {

                if (exception2 is ValidateSoftException)

                {

                    flag = true;

                    exception = exception2 as ValidateSoftException;

                    break;

                }

                builder.Append("\r\n" + exception2.Message);

                builder2.Append("\r\n" + exception2.Source);

                builder3.Insert(0, exception2.StackTrace + "\r\n");

            }

            if (flag)

            {

                builder = new StringBuilder(exception.Message);

                builder2 = new StringBuilder();

                builder3 = new StringBuilder();

            }

            AlertDown(cs, Encoding(builder.ToString()));

        }

 

        public static void ShowError(ClientScriptManager cs, string str)

        {

            AlertDown(cs, Encoding(str));

        }

    }

}

要去还书拉,今天就乱七八糟的先总结这么多吧!唉,真是语无伦次。就先这样啦。