一、使用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);
});