2018年4月23日 星期一

MSSQL 以特定格式取得日期



-- yyyy/mm/dd => 2019/02/28
SELECT CONVERT(VARCHAR, GETDATE(), 111)

-- yyyymmdd => 20190228
SELECT CONVERT(VARCHAR, GETDATE(), 112)

-- hh:mm:ss:mmm(24h) => 22:50:40:017
SELECT convert(varchar, getdate(), 114)

-- yyyy-mm-dd hh:mm:ss(24h) => 2019-02-28 22:50:40
SELECT convert(varchar, getdate(), 120)

-- yyyy-mm-dd hh:mm:ss.mmm => 2019-02-28 22:50:40.020
SELECT convert(varchar, getdate(), 121)

-- yyyy-mm-ddThh:mm:ss.mmm => 2019-02-28T22:50:40.020
SELECT convert(varchar, getdate(), 126)

-- yyyy mm dd => 2019 02 28
SELECT replace(convert(varchar, getdate(), 111), '/', ' ')

-- yyyy-mm => 2019-02
SELECT convert(varchar(7), getdate(), 126)


2018年3月26日 星期一

C# 宣告字串類型的列舉

當在 C# 中需要使用到字串類型的列舉時,你可以試試下列的做法定義 ContentTypeEnum
當定義好之後就可以在主程式中打 ContentTypeEnum. 就會跳出建議選單。


using System;
namespace CSharp_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ContentTypeEnum.Json);
Console.ReadKey();
}
}
public static class ContentTypeEnum
{
public static String X_WWW_Form_Urlencoded { get { return "application/x-www-form-urlencoded"; } }
public static String Json { get { return "application/json"; } }
}
}

2018年2月28日 星期三

C# Http Request

//// 建立連線
WebRequest request = WebRequest.Create(requestUrl);

//// 定義連線資訊
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);

//// FormData
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
streamWriter.Write(parameter);
streamWriter.Flush();
streamWriter.Close();

//// 回應資訊
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

//// Close
reader.Close();
response.Close();

2018年1月31日 星期三

C# 日期字串統一格式

//// 定義日期字串
string dateString = "2018/03/10";

//// 字串 -> DateTime
DateTime datetime = Convert.ToDateTime(dateString);

//// DateTime -> 字串(yyyy-MM-dd HH:mm:ss)
String datetimeString = datetime.ToString("yyyy-MM-dd HH:mm:ss");

//// 轉回來的字串才能統一格式
Console.WriteLine(datetimeString);

參考程式 -> C# Online

2018年1月5日 星期五

SQL Case When In Where Condition

SELECT
   column1, 
   column2
FROM
   viewWhatever
WHERE
CASE 
    WHEN @locationType = 'location' AND account_location = @locationID THEN 1
    WHEN @locationType = 'area' AND xxx_location_area = @locationID THEN 1
    WHEN @locationType = 'division' AND xxx_location_division = @locationID THEN 1
    ELSE 0
END = 1
SELECT column1, column2
FROM viewWhatever
WHERE
    (@locationType = 'location' AND account_location = @locationID)
    OR
    (@locationType = 'area' AND xxx_location_area = @locationID)
    OR
    (@locationType = 'division' AND xxx_location_division = @locationID)
By: https://stackoverflow.com/questions/206484/sql-switch-case-in-where-clause

2017年12月29日 星期五

MSSQL 確認資料是否為當年

SELECT *
FROM MyTable
WHERE YEAR(GETDATE()) = YEAR(@SelectDate)

MSSQL 週一到週五下午五點半過後與假日的規則

DECLARE @SelectDate DateTime = '2017/12/29 17:00';
DECLARE @StartDateTime Datetime = CONVERT(VARCHAR(10), @SelectDate, 111) + ' 17:30:00',
-- 2017-12-11 17:30:00.000 (今天是 2017/12/29 17:00)
@EndDateTime DateTime = CONVERT(VARCHAR(10), @SelectDate + 1, 111);
-- 2017-12-12 00:00:00.000 (今天是 2017/12/29 17:00)
DECLARE @IsFitRull BIT = 1; -- 預設不符合規則

IF (
DATEPART(DW, @SelectDate) IN (1, 7) OR -- 星期六或星期日
(DATEPART(DW, @SelectDate) IN (2, 3, 4, 5, 6) AND
@StartDateTime <= @SelectDate AND @SelectDate < @EndDateTime) -- 星期一 ~ 星期五 17:30 ~ 隔天 00:00
)
BEGIN
SET @IsFitRull = 0;
END
SELECT @IsFitRull

2017年10月13日 星期五

Visual Studio 無法叫用中斷點

解決 Visual Studio 無法叫用中斷點 原始程式碼與原始版本不同

1.把 Visual Studio 關掉
2.把專案中的 bin obj 資料夾刪掉
3.重開 Visual Studio 並建置

完成

2017年10月11日 星期三

Visual Studio 折疊與展開程式碼

(Ctrl + M) + (Ctrl + L)

執行第一次  => 全部展開
執行第二次  => 全部折疊

Kendo 國籍語言包

https://cdnjs.com/libraries/kendo-ui-core

Kendo Ui Loading Mask

kendo.ui.progress($("#id"), true);

Kendo Ui Form 控制項

http://demos.telerik.com/kendo-ui/styling/index

CSS 滑入滑出 translateX

element.style {
  1. transformtranslateX(201px);

Kendo Grid 修改刪除按鈕與功能

columns: [
            { field: "FirstName", title: "First Name" },
            { field: "LastName", title: "Last Name" },
            { field: "Position" },
            { field: "Phone", title: "Phone" },
            { field: "Extension", title: "Ext", format: "{0:#}" },
            { command: [ "edit", "destroy" ] }
        ]

transport: {
                read: {
                    url: crudServiceBaseUrl + "/EmployeeDirectory/All",
                    dataType: "jsonp"
                },
                update: {
                    url: crudServiceBaseUrl + "/EmployeeDirectory/Update",
                    dataType: "jsonp"
                },
                destroy: {
                    url: crudServiceBaseUrl + "/EmployeeDirectory/Destroy",
                    dataType: "jsonp"
                },
                create: {
                    url: crudServiceBaseUrl + "/EmployeeDirectory/Create",
                    dataType: "jsonp"
                },
                parameterMap: function(options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            }

2017年10月5日 星期四

.net mvc 錯誤訊息顯示

<system.web>
    <customErrors mode="Off"/>
</system.web>
<system.webServer>
    <httpErrors errorMode="Detailed" />
</system.webServer>

2017年10月1日 星期日

Kendo Ui Notification

Javascript
// 訊息設定
let popupNotification = null;
const msgHideAfter = 1000;
$(document).ready(function () {
    $("body").append("<span id='popupNotification'></span>");
    popupNotification = $("#popupNotification").kendoNotification({
        autoHideAfter: msgHideAfter,
        templates: [{
            type: "success",
            template: `<div class="msg-success msg">
                            <span class="k-icon k-i-check-outline"></span>           
                            <span class="msg-text">#= message #</span>
                        </div>`
        }, {
            type: "error",
            template: `<div class="msg-error msg">
                            <span class="k-icon k-i-warning"></span>           
                            <span class="msg-text">#= message #</span>
                        </div>`
        }, {
            type: "info",
            template: `<div class="msg-info msg">
                            <span class="k-icon k-i-information"></span>           
                            <span class="msg-text">#= message #</span>
                        </div>`
        }]
    }).data("kendoNotification");
});
// 顯示訊息
function showSuccessMsg(message, callback = null) {
    popupNotification.show({
        message: message
    }, "success");
    if (ifFunctionExist(callback))
    {
        callback();
    }
}
function showErrorMsg(message, callback = null) {
    popupNotification.show({
        message: message
    }, "error");
    if (ifFunctionExist(callback)) {
        callback();
    }
}
function showInfoMsg(message, callback = null) {
    popupNotification.show({
        message: message
    }, "info");
    if (ifFunctionExist(callback)) {
        callback();
    }
}

CSS
.msg {
    width: auto;
    font-size: 25px;
    color: white;
    cursor: pointer;
}

.msg .k-icon {
    font-size: 25px;
    padding-left: 15px;
}

.msg-success {
    background-color: rgb(131, 185, 54);
}

.msg-error {
    background-color: rgb(246, 75, 47);
}

.msg-info {
    background-color: rgb(47, 169, 246);
}

.msg-text {
    margin-left: -10px;
    padding-right: 10px;
}

2017年9月29日 星期五

Validate Input in ASP.NET MVC

[HttpPost]
[ValidateInput(false)]
public ActionResult  ValidateInput(string description)
{
   ValidateModel validateInputModel = new ValidateModel();
   validateInputModel.description = description;
   return View(validateInputModel);
}
來源:https://www.codeproject.com/Tips/753483/Validate-Input-in-ASP-NET-MVC

2017年9月28日 星期四

javascript 特殊符號轉換

function htmlEscape(str) {
    return str
        .replace(/&/g, '&amp;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
}

// I needed the opposite function today, so adding here too:
function htmlUnescape(str){
    return str
        .replace(/&quot;/g, '"')
        .replace(/&#39;/g, "'")
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&amp;/g, '&');
}

remove last word from SQL query

SET @columnSql = SUBSTRING ( @columnSql , 1 , LEN ( @columnSql ) - 1 )