1. 程式人生 > >nodejs操作MSSQL兩種方式--筆記

nodejs操作MSSQL兩種方式--筆記

一、使用MSsql微軟官方npm包。好處微軟原生。弊端:支援不夠強大。

二、使用Edge npm 包,好處夠強大,C#有多強大,他就有多強大。弊端:不能跨平臺。如要跨平臺的等mono的支援吧,具體我也沒有太關注這個東西。

例項①MSsql微軟官方npm包(我同事預研的,直接copy過來了,偷懶。有亂碼,將據點吧)

npm install [email protected] 使用3X的版本4X的升級了 下面的用不了了。
var mssql = require('mssql');
var user = "sa",
    password = "123456",
    server = "127.0.0.1",
    database = "test";

/**
 * Ĭ��config����
 * @type {{user: string, password: string, server: string, database: string, options: {encrypt: boolean}, pool: {min: number, idleTimeoutMillis: number}}}
 */
var config = {
    user: user,
    password: password,
    server: server, // You can use 'localhost\\instance' to connect to named instance
    database: database,
    options: {
        encrypt: true // Use this if you're on Windows Azure
    },
    pool: {
        min: 0,
        idleTimeoutMillis: 3000
    }
};

/**
 * ��ʼ��config
 * @param user
 * @param password
 * @param server
 * @param database
 */
var initConfig = function (user, password, server, database) {
    config = {
        user: user,
        password: password,
        server: server, // You can use 'localhost\\instance' to connect to named instance
        database: database,
        options: {
            encrypt: true // Use this if you're on Windows Azure
        },
        pool: {
            min: 0,
            idleTimeoutMillis: 3000
        }
    }
};

/**
 * �ָ�Ĭ��config
 */
var restoreDefaults = function () {
    config = {
        user: user,
        password: password,
        server: server, // You can use 'localhost\\instance' to connect to named instance
        database: database,
        options: {
            encrypt: true // Use this if you're on Windows Azure
        },
        pool: {
            min: 0,
            idleTimeoutMillis: 3000
        }
    };
};

/**
 * ִ��ԭ��Sql
 * @param sql
 * @params ��������(��Ϊ�գ�Ϊ�ձ�ʾ���Ӳ���)
 * @param callBack(err,recordset)
 */
var querySql = function (sql, params, callBack) {
    var connection = new mssql.Connection(config, function (err) {
        var ps = new mssql.PreparedStatement(connection);
        if (params != "") {
            for (var index in params) {
                if (typeof params[index] == "number") {
                    ps.input(index, mssql.Int);
                } else if (typeof params[index] == "string") {
                    ps.input(index, mssql.NVarChar);
                }
            }
        }
        ps.prepare(sql, function (err) {
            if (err)
                console.log(err);
            ps.execute(params, function (err, recordset) {
                callBack(err, recordset);
                ps.unprepare(function (err) {
                    if (err)
                        console.log(err);
                });
            });
        });
    });
    restoreDefaults();
};

//querySql("select id as roleId,roleName from dbo.role","",function(err,recordset){
//   console.dir(recordset);
//});

/**
 * ��������ѯ
 * @param tableName ����
 * @param topNumber ǰtopNumber��
 * @param whereSql  whereSql
 * @param params    ��ѯ�������

②edge方式:

var edge = require('edge');
var sqlPath = "Data Source=127.0.0.1;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=123456;Connect Timeout=180;Pooling=False";
var helloWorld = edge.func(function () {/*
 async (input) => {
 string str = "2017-12-12 12:12:12";
 return str.Substring(0, 7);
 }
 */});

helloWorld('JavaScript', function (error, result) {
    if (error) throw error;
    console.log(result);
});

//var edge = require('edge');


var getInsuranceChannelType = edge.func('sql', {
    source: function () {/*
     select top 2 * from [InsuranceChannelType]
     */},
    connectionString: sqlPath
});
getInsuranceChannelType(null, function (error, result) {
    if (error) throw error;
    console.log(result);
    //console.log(result[0].ProductName);
    //console.log(result[1].ReorderLevel);
});
/*exports.findById = function (req, res, next) {
    getEmployeeById({ Id: req.params.id }, function (error, data) {
        if (error) {
            console.log(error);
            res.send(error.message);
        }
        if (data) {
            res.send(data);
        }
        else {
            var noData = [];
            res.send(noData);
        }
    });
}*/
var getTop10Products = edge.func('sql', {
    source: function () {/*
     select top 1 * from [InsuranceCompany]
     */
    },
    connectionString: sqlPath
});
getTop10Products(null, function (error, result) {
    if (error) throw error;
    console.log(result);
   //console.log(result[0].ProductName);
   // console.log(result[1].ReorderLevel);
});

相關推薦

nodejs操作MSSQL方式--筆記

一、使用MSsql微軟官方npm包。好處微軟原生。弊端:支援不夠強大。 二、使用Edge npm 包,好處夠強大,C#有多強大,他就有多強大。弊端:不能跨平臺。如要跨平臺的等mono的支援吧,具體我也沒有太關注這個東西。 例項①MSsql微軟官方npm包(我同事預研的,直接

基於aspectj實現AOP操作方式——xml配置

aop ont 方式 alt 分享圖片 文件 分享 spa 技術分享 1. 要導入的 jar 包: 常用的aspectj表達式: 權限修飾符可以省略,以下表示:返回值類型為任意,com.chy.service包以及其子包下的、任意類的、參數任意的、任意方法 ex

操作shell方式

con imp != auth tcp 輸出 div fat run 交互式shellpackage main import ( "golang.org/x/crypto/ssh" "log" "os" "fmt" ) fu

例項講解Springboot整合MongoDB進行CRUD操作方式

1 簡介 Springboot是最簡單的使用Spring的方式,而MongoDB是最流行的NoSQL資料庫。兩者在分散式、微服務架構中使用率極高,本文將用例項介紹如何在Springboot中整合MongoDB的兩種方法:MongoRepository和MongoTemplate。 程式碼結構如下: 2 專

Lua 調用的 C 函數保存 state 的方式: Storing State in C Functions 筆記

.com 包裝 ram targe cti gist 方式 機制 key http://yanbin.is-programmer.com/posts/94214.html Registery的Key 1. 整數Key用於Lua的引用機制,所以不要使用整數作為Key

文件操作(三)——文件的修改方式

none new enc ast view 決定 cnblogs utf-8 logs #文件的修改兩種方式:# 1、讀入內存,再寫;# 2、硬盤模式決定,將文件先讀出來,寫完再另存,邊讀邊寫。這樣就需要打開兩個文件 1 f=open(‘my_heart_will_go

python-文件操作-修改的方式

import str2 replace new col 檢查 循環 blog nbsp 方法一:(占內存) 全部把文件讀進內存進行修改。 f_name ="D:/聯系方式2.txt" old_str =‘喬一菲‘ new_str =‘剛亮‘ f =open(f_nam

javascript教程系列40:DOM中操作樣式的方式

AS color 單位 css 註意 pan col ntb javascrip 1 DOM中操作樣式的兩種方式 1 通過元素的style屬性 註意: 通過style屬性設置樣式時,css中要寫單位的屬性,在js代碼中也要加單位 //html <div id="bo

操作系統,編程語言分類,執行python方式,變量,內存管理,定義變量的三個特征

什麽 height 取代 沒有 一個 Coding 開發 軟件 簡單 操作系統 1、什麽是操作系統 操作系統位於計算機硬件與應用軟件之間 是一個協調、管理、控制計算機硬件資源與軟件資源的控制程序2、為何要有操作系統? 1、控制硬件 2、把對硬件的復雜

用sql語句操作mysql資料庫的增刪改查(命令提示符和引入mysql模組方式

1:命令提示符方法: 我的 :mysql.sql檔案。當然首先得開啟本地伺服器(我的是xampp); 少了一步了:首先要在進入mysql -h127.0.0.1 -u root -p;後引入我的mysql.sql 檔案; 我用的是 source方法:  source

POI操作Excel詳解,HSSF和XSSF方式

HSSF方式: package com.tools.poi.lesson1; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; impor

SpringBoot學習筆記(二) SpringBoot專案建立的方式

叄念 springboot 專案建立方式其實有多種,這裡我們主要介紹兩種方式: 當然這裡建議大家用方式一來建立,方式二用於理解 方式

spring之AOP操作(基於aspectJ實現)--配置檔案和註解方式實現

AOP概念   1 aop:面向切面(方面)程式設計,擴充套件功能不修改原始碼實現     2  AOP採取橫向抽取機制,取代了傳統縱向繼承體系重複性程式碼     3 aop底層使用動態代理實現     (1)第一種情況,有介面情況,使用動態代理建立介面實現類代理物

java ssdb 操作link遍歷map的方式

方法一:不推薦使用,只會返回map中的key Link link = ssdb.link; resp = link.request("qpush", "q", "a"); for(int i=1; i<resp.raw.size(); i+=2){ String s = ne

java操作excel常用的方式

***************************************************** Excel是我們平時工作中比較常用的用於儲存二維表資料的,JAVA也可以直接對Excel進行操作,在這篇部落格中將為大家介紹兩種操作Excel的方式,分別為:jx

不使用spring的情況下用java原生程式碼操作mongodb資料庫的方式

由於更改了mongodb3.0資料庫的密碼,導致這幾天storm組對資料進行處理的時候,一直在報mongodb資料庫連線不上的異常。 主要原因實際上是和mongodb本身無關的,因為他們改的是配

Java Spark之建立RDD的方式操作RDD

首先看看思維導圖,我的spark是1.6.1版本,jdk是1.7版本  spark是什麼?  Spark是基於記憶體計算的大資料平行計算框架。Spark基於記憶體計算,提高了在大資料環境下資料處理的實時性,同時保證了高容錯性和高可伸縮性,允許使用者將Spark 部署在大量廉

SQL SERVER 資料庫表同步複製 筆記 SqlServer2008 資料庫同步的方式 (釋出、訂閱)

SQL SERVER 資料庫表同步複製 筆記 同步複製可執行在不同版本的SQL Server服務之間 環境模擬需要兩臺資料庫192.168.1.1(釋出),192.168.1.10(訂閱) 1、在釋出和訂閱機中建立兩個相同的賬戶(如: sqladmin),使用者名稱和密碼都一致,隸屬於administr

Python筆記——多執行緒實現的方式及讓多條命令併發執行

一 概念介紹         Thread 是threading模組中最重要的類之一,可以使用它來建立執行緒。有兩種方式來建立執行緒:一種是通過繼承Thread類,重寫它的run方法;另一種是建立一個threading.Thread物件,在它的初始化函式(__i

Solr6.2.1 學習筆記(一)搭建執行環境(方式)

1.準備工作 Java JDK8 下載並安裝jdk1.8(solr6.*是基於JDK1.8開發的) tomcat8.0 下載並解壓tomcat8 2.在jetty伺服器下執行 solr解壓之後目錄結構如下: solr5之後釋出版本自帶jetty