.NET開發常用知識點總結匯總

暖楓無敵發表於2016-04-07

1、批量插入資料SQL語句

declare @i int
set @i=1
while @i<=400
begin
insert into GGJ_Sys_Users(UserName,Pwd,UserTrueName,Gender,Birthday,UnitType,UnitCode,UnitName,CreateUserID) 
values('admin'+cast(@i as varchar),'21232F297A57A5A743894A0E4A801FC3','admin'+cast(@i as varchar),'男','1985-01-12',9,'100007','行政部',1)
set @i=@i+1
end

2、比較兩個DataTable,取交集和差集

定義一個實體類,主要是一個建構函式和比較器,程式碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace ProvincePlat.CommonClass
{
    public class AccessedUnit
    {
        public string F_DataCenterID { get; set; }
        public string F_DataCenterName { get; set; }
        public string BuildUnitName { get; set; }
        public string F_Date { get; set; }

        public AccessedUnit(string f_DataCenterID, string f_DataCenterName, string buildUnitName, string f_Date)
        {
            this.F_DataCenterID = f_DataCenterID;
            this.F_DataCenterName = f_DataCenterName;
            this.BuildUnitName = buildUnitName;
            this.F_Date = f_Date;
        }

    }


    class AccessedUnitEquality : IEqualityComparer<AccessedUnit>
    {
        public bool Equals(AccessedUnit x, AccessedUnit y)
        {
            return x.F_DataCenterID == y.F_DataCenterID;
        }


        public int GetHashCode(AccessedUnit obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return obj.ToString().GetHashCode();
            }
        }
    }  
}

GetAccessedUnit.ashx,邏輯程式碼如下:

using ProvincePlat.CommonClass;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;


namespace ProvincePlat.WebPages.EnergyConsumptionPandect
{
    /// <summary>
    /// GetAccessedUnit 的摘要說明
    /// </summary>
    public class GetAccessedUnit : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("[");
            string datestarttime = System.Configuration.ConfigurationManager.AppSettings["AccessTime"].ToString();
            string days = System.Configuration.ConfigurationManager.AppSettings["PreDays"].ToString();
            int day = int.Parse(days);
            string datetimes = DateTime.Now.AddDays(-day).ToString("yyyy-MM-dd");
            string datetimee = DateTime.Now.ToString("yyyy-MM-dd");
            StringBuilder sb = new StringBuilder();
            string sql = string.Format("EXEC GGJ_GET_AccessedUnit  '{0}','{1}'", datestarttime, datetimes);
            string sql2 = string.Format("EXEC GGJ_GET_AccessedUnit  '{0}','{1}'", datetimes, datetimee);
            DataTable dt = CommonClass.QuerySQL.GetDataTable(sql);
            DataTable dt2 = CommonClass.QuerySQL.GetDataTable(sql2);
            IList<AccessedUnit> accessed = new List<AccessedUnit>();
            IList<AccessedUnit> accessed2 = new List<AccessedUnit>();
            foreach (DataRowView drv in dt.DefaultView)
            {
                accessed.Add(new AccessedUnit(drv["F_DataCenterID"].ToString(), drv["F_DataCenterName"].ToString(), drv["BuildUnitName"].ToString(), drv["F_Date"].ToString()));
            }
            foreach (DataRowView drv in dt2.DefaultView)
            {
                accessed2.Add(new AccessedUnit(drv["F_DataCenterID"].ToString(), drv["F_DataCenterName"].ToString(), drv["BuildUnitName"].ToString(), drv["F_Date"].ToString()));
            }
            var jiaoji = accessed.Intersect(accessed2, new AccessedUnitEquality()).ToList();//交集   
            var chaji = accessed.Except(accessed2, new AccessedUnitEquality()).ToList();//差集  

            foreach (AccessedUnit unit in jiaoji)
            {
                sb.Append("{\"id\":\"" + unit.F_DataCenterID);//單位編碼
                sb.Append("\"},");
            }
            string jsonString = sb.ToString().TrimEnd(',');
            jsonBuilder.Append(jsonString);


            jsonBuilder.Append("]");
            string jsonString2 = jsonBuilder.ToString();
            context.Response.Write(jsonString2);
            context.Response.End();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


Ajax呼叫

//獲取已接入單位數量
function getAccessedUnitCNT(unitcode) {
        var count = 0;
        $.ajax({
            url: 'GetAccessedUnit.ashx?rd=' + Math.random(),
            type: 'GET',
            async: false,//取消預設非同步為true,設定同步狀態為false
            data: 'dname=' + escape(unitcode),
            datatype: 'xml',
            success: function (data) {
                var datas = eval(data);
                //alert(data);
                //alert("單位個數:" + datas.length);
                if (data != null && data != "") {
                    count = datas.length;
                }
            }
      });
     return count;
}

3、BootStrap整體結構圖



4、文字輸入框輸入完後,按回車觸發按鈕查詢

    為input控制元件增加onkeypress事件,執行函式名為EnterTextBox()


    <input type="text" tabindex="1" class="input-query"  onkeypress="return EnterTextBox();"  id="input_queryCondition" />

    <img src="../../Images/map/search_icon.png" id="btn_query" tabindex="2" title="查詢" onclick="clickSearchResult();" />



 function EnterTextBox() {
     if (event.keyCode == 13) {
             event.keyCode = 9;
             event.returnValue = false;
             document.all["btn_query"].click();
     }
 }


5、JavaScript計算兩個日期的時間差

正規表示式替換Javascript日期/改為- 

日期 = 日期.replace(/\//g, "\-");

有時候我們需要知道兩個日期之間差了多少天,多少小時,甚至多少分鐘多少秒。下面我們用JavaScript實現一個函式,用於計算兩個日期的時間差,先來看看程式碼:

/* 
* 獲得時間差,時間格式為 年-月-日 小時:分鐘:秒 或者 年/月/日 小時:分鐘:秒 
* 其中,年月日為全格式,例如 : 2010-10-12 01:00:00 
* 返回精度為:秒,分,小時,天
*/

function GetDateDiff(startTime, endTime, diffType) {
    //將xxxx-xx-xx的時間格式,轉換為 xxxx/xx/xx的格式 
    startTime = startTime.replace(/\-/g, "/");
    endTime = endTime.replace(/\-/g, "/");
    //將計算間隔類性字元轉換為小寫
    diffType = diffType.toLowerCase();
    var sTime = new Date(startTime);      //開始時間
    var eTime = new Date(endTime);  //結束時間
    //作為除數的數字
    var divNum = 1;
    switch (diffType) {
        case "second":
            divNum = 1000;
            break;
        case "minute":
            divNum = 1000 * 60;
            break;
        case "hour":
            divNum = 1000 * 3600;
            break;
        case "day":
            divNum = 1000 * 3600 * 24;
            break;
        default:
            break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}

var result = GetDateDiff("2010-02-26 16:00:00""2011-07-02 21:48:40""day");

使用的方法很簡單,比如計算天數可以這樣:

GetDateDiff("2010-02-26 16:00:00""2011-07-02 21:48:40""day");

計算秒數則可以這樣: 

GetDateDiff("2010-02-26 16:00:00""2011-07-02 21:48:40""second");


6、


相關文章