Nodejs loopback框架笔记(四):如何对sql server数据库使用SQL语句

如何在loopback框架中对sql server数据库使用SQL语句

一、安装mssql 插件

使用以下cmd命令安装sql server插件

cmd npm install mssql --save

二、使用方法直接上代码

// 调用方法
addAllData();

// 执行方法
function addAllData() {
    
    
    var sql = require('mssql');
    // 需要执行的SQL语句
    var sqlStatement = "select * from dbo.NCP_WT_VIEW where DATEDIFF(day,CurrDate,getdate()) = 0";
 	sql.connect("mssql://账号:密码@主机名/database").then(function (connection) {
    
    
		new sql.Request().query(sqlStatement).then(function (result){
    
    
            // result 为执行sql语句的返回结果
            console.log(result);
            doRelease(connection);
        }).catch(function (err) {
    
    
            console.log(err);
            // 关闭连接
            doRelease(connection);
        });
    }).catch(function (err) {
    
    
    	// 关闭连接
        console.log(err);
    });

	// 关闭连接
	function doRelease(connection) {
    
    
		connection.close(
			function (err) {
    
    
			    if (err) {
    
    
			        console.log(err);
			    }
		});
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44104809/article/details/104614484