Win10 + node.js + SQL Server 2019 Express で Tedious Request を少し試してみる

Win10 + node.js + SQL Server 2019 Express で Tedious Request を少し試してみる
#目的
・[Tedious Request](https://tediousjs.github.io/tedious/api-request.html)
・[Package - mssql-tedious-int64](https://developer.aliyun.com/mirror/npm/package/mssql-tedious-int64)
を参考に少し試してみる(select count(*) & select ～ order by)
対象はWin10 + node.js -> Ubuntu 18.04 + SQL Server 2019 Express 
#対象テーブル
データの並びは以下に準拠
<a href="http://www.post.japanpost.jp/zipcode/dl/readme.html">郵便番号データの説明</a>
対象データにユニークキーになりそうなデータが無いので
8桁のテキストを追加する

```sql
CREATE TABLE [dbo].[ZIPCODE](
	[SEQ] [nchar](8) NOT NULL,
	[PREFCODE] [nchar](3) NULL,
	[KUBUNCODE] [nchar](8) NULL,
	[POSTAL5] [nchar](5) NULL,
	[POSTAL] [nchar](8) NULL,
	[PREFKANA] [nchar](20) NULL,
	[CITYKANA] [nchar](40) NULL,
	[ADDRKANA] [nchar](80) NULL,
	[PREFKANJI] [nchar](20) NULL,
	[CITYKANJI] [nchar](40) NULL,
	[ADDRKANJI] [nchar](80) NULL,
	[FLG1] [int] NULL,
	[FLG2] [int] NULL,
	[FLG3] [int] NULL,
	[FLG4] [int] NULL,
	[FLG5] [int] NULL,
	[FLG6] [int] NULL
) ON [PRIMARY]
```

#サンプルコード
地元県だと名前感あるので 17ISHIKA.CSV を使う

```js
var Connection = require('tedious').Connection; 
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

var connectionConfig = {
    userName: 'demo',
    password: 'demo',
    server: '192.168.5.49',
    options: {
        database: 'demo'
    }
};

var connection = new Connection(connectionConfig);

connection.on('connect', function(err) {  
    if (err) {
        console.log(err);
        console.log('err:connect');
        connection.exit;
        process.exit(0);
    }
    console.log("Connected");

    itemcount();
    console.log("done:truncate");  
});

function itemcount() {
    request = new Request("SELECT COUNT(*) FROM ZIPCODE;", function(err, rowCount) {
        if (err) {
            console.log(err);
            console.log('err:request:itemcount');
            connection.exit;
            process.exit(0);
       }
    });

    request.on('row', function(columns) {
        console.log('value: ' + columns[0].value);
    });

    request.on('doneInProc', function (rowCount, more, rows) {  
        console.log('doneInProc:itemcount: '+ rowCount + ' row(s) returned');  
    });
 
    request.on('requestCompleted', function (err) { 
        console.log("requestCompleted:itemcount");
        itemselect()
    });

    connection.execSql(request); 
}

function itemselect() {
    request = new Request("SELECT * FROM ZIPCODE WHERE SEQ < '00000003' ORDER BY SEQ DESC;", function(err, rowCount) {
        if (err) {
            console.log(err);
            console.log('err:request:itemselect');
            connection.exit;
            process.exit(0);
       }
    });
    // 1行毎にイベントが発生する
    request.on('row', function(columns) {
        console.log('value: ' + columns[0].value);
        console.log('value: ' + columns.length);
        // var i = 0;
        // while (i < columns.length) {
        //     console.log(columns[i++].value);
        // }
    });

    request.on('doneInProc', function (rowCount, more, returnStatus, rows) {  
        console.log('doneInProc: '+ rowCount + ' row(s) returned');  
    });

    request.on('requestCompleted', function (err) { 
        console.log("requestCompleted:itemselect");
        connection.exit;
        process.exit(0);
    });

    connection.execSql(request); 
}
```
#参考にしたのは以下のサイト
[Tedious Request](https://tediousjs.github.io/tedious/api-request.html)
[Package - mssql-tedious-int64](https://developer.aliyun.com/mirror/npm/package/mssql-tedious-int64)
[Win10 + node.js から SQL Server 2019 Express に CSVファイルを書き込んでみる](https://qiita.com/tabizou/items/a56bba5162614d381cb6)
[Win10 + node.js から SQL Server 2019 Express に CSVファイルを書き込んでみる 修正版](https://qiita.com/tabizou/items/759105b271c419fe6683)