nodejs操作MSSQL两种方式--笔记
来源:互联网 发布:淘宝网运动鞋女 编辑:程序博客网 时间:2024/05/20 07:17
一、使用MSsql微软官方npm包。好处微软原生。弊端:支持不够强大。
二、使用Edge npm 包,好处够强大,C#有多强大,他就有多强大。弊端:不能跨平台。如要跨平台的等mono的支持吧,具体我也没有太关注这个东西。
实例①MSsql微软官方npm包(我同事预研的,直接copy过来了,偷懒。有乱码,将据点吧)
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);});
0 0
- nodejs操作MSSQL两种方式--笔记
- nodejs 连接操作MSSQL 数据库
- linux 下部署nodejs(两种方式)
- linux 下部署nodejs(两种方式)
- nodejs两种方式实现跨域(jsonp,cors)
- nodejs 文件操作笔记
- Android 两种异步操作方式
- php mysqli两种操作方式示例
- 两种cookie操作方式代码
- C#文件操作的两种方式
- Extjs cookie操作的两种方式
- 指针操作数组的两种方式
- MyBatis操作数据库的两种方式
- SQL server(MSSQL)客户端工具登录数据库的两种命令行登录方式
- nodejs中edje-sql 读取MSSQL数据-笔记
- 【笔记】HashMap的两种遍历方式
- C51两种方式点亮LED(总线操作,位操作)
- iOS文件操作两种方式:NSFileManager和流操作
- iframe加载顺序导致数据访问出现问题
- leetcode : jumpgame
- MAC地址和IP地址
- TYVJ1415 差分约束
- TPL——开始一个Task
- nodejs操作MSSQL两种方式--笔记
- 设计模式学习笔记——观察者模式
- pat1014 Waiting in line
- 357. Count Numbers with Unique Digits
- java运行顺序
- ubuntu 14.04 编译android6.01 (freescale-imx6)
- 什么是经验贝叶斯克里金插值法?
- Leetcode:Find the Difference
- Redis的文档