2019年3月13日 星期三

[Javascript] form to json string

在開發前端時,有時需要把畫面的輸入欄位匯成JSON傳送
若要自行處理,需要每一個輸入元件逐一取出
導致程式過於冗長,重複取值的寫法遍滿各區塊

另外,如果環境沒有jquery,變成要原生javascript處理
後來查找,稍微改成下方的function
傳入callName,可以將class內的元件轉成json string

 function toJSONString(className) {
    let obj = {};

    let elements = document.querySelector("." + className).querySelectorAll("input, select, textarea");
    for (let i = 0; i < elements.length; ++i) {
        let element = elements[i];
        let key = !element.id ? element.name : element.id; // 取得id或是name
        let value = element.value;

        if (key) {
            obj[key] = value;
        }
    }
    return JSON.stringify(obj);
}

Ref: https://codepen.io/gabrieleromanato/pen/LpLVeQ
https://www.lvh.io/posts/queryselectorall-from-an-element-probably-doesnt-do-what-you-think-it-does.html



2019年3月12日 星期二

[JavaScript] Hide Url Parameter

最近遇到一個需求,需要將網址的參數拿掉
找了一些作法,可能要把參數放在cookie, session
或是塞DB,再從DB取資料出來,這都還要加工處理

後來查到github上面有人分享,更改history的狀態就可以
只要在一開始呼叫這個function,便可以參數隱藏了

function getURLParameter(name) {
 return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
function hideURLParams() {
 //Parameters to hide (ie ?success=value, ?error=value, etc)
 var hide = ['success','error'];
 for(var h in hide) {
  if(getURLParameter(h)) {
   history.replaceState(null, document.getElementsByTagName("title")[0].innerHTML, window.location.pathname);
  }
 }
}

Ref: https://gist.github.com/ScottKaye/5158488#file-hide-url-parameters-js

2019年3月6日 星期三

[SQL Server]import large sql file

在執行少量的sql寫入時,可以直接貼到Microsoft SQL Server Management Studio執行
但若是檔案太大時,可以使用sqlcmd的方式來執行
指令如下:

sqlcmd -S 資料庫IP -U 使用者名稱 -P 使用者密碼 -d 資料庫名稱 -i .sql檔案路徑

執行時會跳出錯誤,需開啟QUOTED_IDENTIFIER:
訊息 1934, 層級 16, 狀態 1, 伺服器 User, 行 4
INSERT 失敗,因為下列 SET 選項的設定錯誤: 'QUOTED_IDENTIFIER'。請確認與 索引檢視表及/或計算資料行上的索引及/或篩選的索引及/或查詢通知及/或 XML 資料類型方法及/或空間索引作業 一起使用的 SET 選項是否正確。


example:
C:\>sqlcmd -I -S localhost -U test -P test123 -d dbo -i C:\sql.sql 


另外,若資料當中有中文,sqlcmd會直接結束
sqlcmd執行必須加入 -f 65001 代表對應中文編碼
SQL當中的中文欄位前方也要加 N
  
最後sqlcmd執行的參數會變成這樣子
若要將執行的結果匯出成txt檔,可以加上-o C:\sql.log example:
C:\>sqlcmd -I -S localhost -U test -P test123 -d dbo -f 65001 -i C:\sql.sql -o C:\sql.log





Ref:
http://blog.aihuadesign.com/2014/01/15/solve-sql-file-too-large-to-import-to-sql-server/
https://docs.microsoft.com/zh-tw/sql/tools/sqlcmd-utility?view=sql-server-2017
http://www.anujchaudhary.com/2011/10/sqlcmd-quotedidentifier-is-off.html
https://blog.csdn.net/roy_88/article/details/52595854


sqlcmd  
   -a packet_size 
   -A (dedicated administrator connection) 
   -b (terminate batch job if there is an error) 
   -c batch_terminator 
   -C (trust the server certificate) 
   -d db_name 
   -e (echo input) 
   -E (use trusted connection) 
   -f codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]
   -g (enable column encryption)
   -G (use Azure Active Directory for authentication)
   -h rows_per_header 
   -H workstation_name 
   -i input_file 
   -I (enable quoted identifiers) 
   -j (Print raw error messages)
   -k[1 | 2] (remove or replace control characters) 
   -K application_intent 
   -l login_timeout 
   -L[c] (list servers, optional clean output) 
   -m error_level 
   -M multisubnet_failover 
   -N (encrypt connection) 
   -o output_file 
   -p[1] (print statistics, optional colon format) 
   -P password 
   -q "cmdline query" 
   -Q "cmdline query" (and exit) 
   -r[0 | 1] (msgs to stderr) 
   -R (use client regional settings) 
   -s col_separator 
   -S [protocol:]server[instance_name][,port] 
   -t query_timeout 
   -u (unicode output file) 
   -U login_id 
   -v var = "value" 
   -V error_severity_level 
   -w column_width 
   -W (remove trailing spaces) 
   -x (disable variable substitution) 
   -X[1] (disable commands, startup script, environment variables, optional exit) 
   -y variable_length_type_display_width 
   -Y fixed_length_type_display_width 
   -z new_password  
   -Z new_password (and exit) 
   -? (usage)
 
 sqlcmd            [-U 登入識別碼]          [-P 密碼]
  [-S 伺服器]            [-H 主機名稱]          [-E 信任連線]
  [-N 加密連線][-C 信任伺服器憑證]
  [-d 使用資料庫名稱] [-l 登入逾時]     [-t 查詢逾時]
  [-h 標頭]           [-s 資料行分隔符號]           [-w 螢幕寬度]
  [-a 封包大小]        [-e 回應輸入]        [-I 啟用引號識別項]
  [-c cmdend]            [-L[c] 伺服器清單[清除輸出]]
  [-q "命令行查詢"]   [-Q "命令行查詢" 並結束]
  [-m 錯誤等級]        [-V 嚴重性等級]     [-W 移除句尾空格]
  [-u unicode 輸出]    [-r[0|1] 訊息傳至 stderr]
  [-i 輸入檔]         [-o 輸出檔]        [-z 新密碼]
  [-f <字碼頁> | i:<字碼頁>[,o:<字碼頁>]] [-Z 新密碼並結束]
  [-k[1|2]移除 [replace] 控制項字元]
  [-y 可變長度類型顯示寬度]
  [-Y 固定長度類型顯示寬度]
  [-p[1] 列印統計資料[冒號格式]]
  [-R 使用用戶端地區設定]
  [-K 應用程式意圖]
  [-M 多重子網路容錯移轉]
  [-b 批次中止錯誤時發生]
  [-v var = "值"...]  [-A 專屬的管理連線]
  [-X[1] 停用命令、啟動指令碼、環境變數 [並結束]]
  [-x 停用變數替代]
  [-j 列印原始錯誤訊息]
  [-g 啟用資料行加密]
  [-G 為驗證使用 Azure Active Directory]
  [-? 顯示語法摘要]

2019年3月5日 星期二

[SQL Server]帳號登入錯誤

在SQL Server新建登入帳號後,登入會跳出錯誤
是因為少設定SQL Server和Windows混和驗證

先以Windows方式登入後,點databse右鍵 -> 屬性



伺服器驗證選擇 "SQL Server和Windows混和驗證"
在重啟SQL Server,就可以使用帳號密碼登入

 


2019年3月4日 星期一

[SQL SERVER] 使用mdf檔還原

手上拿到mdf檔要還原時,要使用附加的方式

1.將mdf檔複製到SQL Server資料庫存放路徑底下

資料庫檔案都是放在 C:\Program Files\Microsoft SQL Server\{version}\MSSQL\Data 目錄
 {version}要看目前您使用資料庫的版本


2.使用附加的方式,加入複製的檔案



Ref: https://ithelp.ithome.com.tw/questions/10046158