在nodejs中,mssql模块支持sqlserver数据库操作。今天将mssql模块的某些功能封装为一个类,方便以后调用。封装的功能有执行存储过程,执行查询语句操作等。如果本篇文章对大家有帮助,那就再好不过了!

要使用mssql模块,请先用npm加载到项目中。加载过程:打开cmd命令框,定位到项目的根目录下,输入npm install mssql --save ,然后按回车键就OK!

封装的代码如下:

//导入mssql模块
var mssql=require("mssql"); var sql={}; //连接参数配置
var config={
user:"sa",
password:"wsjun123456",
server:"localhost", // You can use 'localhost\\instance' to connect to named instance
database:"mydb",
stream:false, // You can enable streaming globally
/*option:{
encrypt:true //Use this if you're on Windows Azure
},*/
pool:{
min:,
idleTimeoutMillis:
}
}; sql.sqlserver=mssql; //sql参数的类型
sql.direction={
//输入参数
Input:"input",
//输出参数
Output:"output",
//返回参数
Return:"return"
}; /**
* 初始化连接参数
* @param {string} user 用户名
* @param {string} password 密码
* @param {string} server 服务器地址
* @param {string} database 数据库名称
*/
sql.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,
stream:false,
/*option:{
encrypt:true //Use this if you're on Windows Azure
},*/
pool:{
min:,
idleTimeoutMillis:
}
};
} /**
* 执行存储过程
* @param {string} procedure 存储过程名称
* @param {JSON} params 存储过程参数
* params的定义格式如:
var params={
//ID是存储过程的第一个参数,要去掉@符号
ID:{
//sqlType是该ID参数在sqlserver中的类型
sqlType:sql.sqlserver.Int,
//direction是表明ID参数是输入还是输出(output)参数
direction:sql.direction.Input,
//该ID参数的值
inputValue:1
},
//Name是存储过程的第二个参数,要去掉@符号
Name:{
sqlType:sqlHelper.sqlserver.Int,
direction:sqlHelper.direction.Output,
outputValue:null
}
};
* @param {function} func 回调函数 共有四个参数 error:错误信息 recordsets:查询的表结果 returnValue:存储过程的返回值 affected:影响的行数
*/
sql.execute=function(procedure,params,func){
try {
var connection = new mssql.Connection(config, function (error) {
if(error)
func(error);
else {
var request = new mssql.Request(connection);
//request.verbose=true;
if (params != null) {
for (var index in params) {
if (params[index].direction == sql.direction.Output) {
request.output(index, params[index].sqlType);
}
else {
request.input(index, params[index].sqlType, params[index].inputValue);
}
}
}
request.execute(procedure, function (error, recordsets, returnValue, affected) {
if (error)
func(error);
else {
for (var index in params) {
if (params[index].direction == sql.direction.Output) {
params[index].outputValue = request.parameters[index].value;
}
}
func(error, recordsets, returnValue, affected);
}
});
}
}); connection.on("error", func); }catch(e){
func(e);
}
}; /**
* 执行sql文本(带params参数)
* @param {string} sqltext 执行的sql语句
* @param {JSON} params sql语句中的参数
* @param {function} func 回调函数 共有三个参数 error:错误消息 recordsets:查询的结果 affected:影响的行数
*/
sql.queryWithParams=function(sqltext,params,func){
try {
var connection = new mssql.Connection(config, function (err) {
if(err)
func(err);
else {
var request = new mssql.Request(connection);
request.multiple=true; if(params){
for(var index in params){
request.input(index,params[index].sqlType,params[index].inputValue);
}
} request.query(sqltext, func);
}
});
connection.on("error",func);
}catch(e){
func(e);
}
}; /**
* 执行sql文本
* @param {string} sqltext 执行的sql语句
* @param {function} func 回调函数 共有三个参数 error:错误消息 recordsets:查询的结果 affected:影响的行数
*/
sql.query=function(sqltext,func){
sql.queryWithParams(sqltext,null,func);
}; /**
* 执行大批量数据的插入
* @param {sqlserver.Table} table 需要插入的数据表
* 数据表的定义如下:
var table=new sql.sqlserver.Table('UserInfoTest');
table.create=true;
table.columns.add('name',sqlHelper.sqlserver.NVarChar(50),{nullable:true});
table.columns.add('pwd',sqlHelper.sqlserver.VarChar(200),{nullable:true});
table.rows.add('张1','jjasdfienf');
table.rows.add('张2','jjasdfienf');
table.rows.add('张3','jjasdfienf');
* @param {function} func 回调函数 共有两个参数 error:错误信息 rowcount:插入数据的行数
*/
sql.bulkInsert=function(table,func){
try{
if(table){
var connection=new mssql.Connection(config,function(err){
if(err) func(err)
else{
var request=new mssql.Request(connection);
request.bulk(table,func);
}
});
connection.on("error",func);
}
else
func(new ReferenceError('table parameter undefined!'));
}catch (e){
func(e);
}
}; /**
* 如果需要处理大批量的数据行,通常应该使用流
* @param {string} sqltext 需要执行的sql文本
* @param {JSON} params 输入参数
* @param {JSON} func 表示一个回调函数的JSON对象,如下所示:
* {
error:function(err){
console.log(err);
},
columns:function(columns){
console.log(columns);
},
row:function(row){
console.log(row);
},
done:function(affected){
console.log(affected);
}
*/
sql.queryViaStreamWithParams=function(sqltext,params,func){
try{
config.stream=true; mssql.connect(config,function(err){
if(err)
func.error(err);
else{
var request=new mssql.Request();
request.stream=true;// You can set streaming differently for each request
if(params){
for(var index in params){
request.input(index,params[index].sqlType,params[index].inputValue);
}
} request.query(sqltext); request.on('recordset',function(columns){
//columns是一个JSON对象,表示 返回数据表的整个结构,包括每个字段名称以及每个字段的相关属性
//如下所示
/*
{ id:
{ index: 0,
name: 'id',
length: undefined,
type: [sql.Int],
scale: undefined,
precision: undefined,
nullable: false,
caseSensitive: false,
identity: true,
readOnly: true },
name:
{ index: 1,
name: 'name',
length: 100,
type: [sql.NVarChar],
scale: undefined,
precision: undefined,
nullable: true,
caseSensitive: false,
identity: false,
readOnly: false },
Pwd:
{ index: 2,
name: 'Pwd',
length: 200,
type: [sql.VarChar],
scale: undefined,
precision: undefined,
nullable: true,
caseSensitive: false,
identity: false,
readOnly: false } }
*/
func.columns(columns);
}); request.on('row', function(row) {
//row是一个JSON对象,表示 每一行的数据,包括字段名和字段值
//如 { id: 1004, name: 'jsw', Pwd: '12345678' }
//如果行数较多,会多次进入该方法,每次只返回一行
func.row(row);
}); request.on('error', function(err) {
//err是一个JSON对象,表示 错误信息
//如下所示:
/*
{ [RequestError: Incorrect syntax near the keyword 'from'.]
name: 'RequestError',
message: 'Incorrect syntax near the keyword \'from\'.',
code: 'EREQUEST',
number: 156,
lineNumber: 1,
state: 1,
class: 15,
serverName: '06-PC',
procName: '' }
*/
func.error(err);
}); request.on('done', function(affected) {
//affected是一个数值,表示 影响的行数
//如 0
//该方法是最后一个执行
func.done(affected);
});
}
}); mssql.on('error',func.error);
}catch(e){
func.error(e);
}finally{
config.stream=false;
}
}; /**
* 如果需要处理大批量的数据行,通常应该使用流
* @param {string} sqltext 需要执行的sql文本
* @param {JSON} func 表示一个回调函数的JSON对象,如下所示:
* {
error:function(err){
console.log(err);
},
columns:function(columns){
console.log(columns);
},
row:function(row){
console.log(row);
},
done:function(affected){
console.log(affected);
}
*/
sql.queryViaStream=function(sqltext,func){
sql.queryViaStreamWithParams(sqltext,null,func);
}; module.exports=sql;

本篇文章是根据https://www.npmjs.com/package/mssql来写作的,不足之处,请勿见怪,本人新手!

最新文章

  1. android.os.NetworkOnMainThreadException异常
  2. 【JavaEE企业应用实战学习记录】MyGetAttributeListener
  3. sqoop sample code
  4. React表单组件自定义-可控及不可控组件
  5. 【Unity3D】自动寻路(Nav Mesh Agent组件)
  6. SQL Server 2008数据库创建,备份,还原图解及注意点
  7. Delphi XE5 Device compatibility
  8. STM32学习之路-LCD(3)<显示图片>
  9. Windbg调试(关于句柄表的获取,32位)
  10. 升讯威微信营销系统开发实践:(3)功能介绍与此项目推广过程的一些体会( 完整开源于 Github)
  11. ios copy和strong,浅拷贝和深拷贝
  12. Docker中安装WordPress
  13. 前端开发【第四篇: Dom操作】
  14. 改造 Android 官方架构组件 ViewModel
  15. “一切都是消息”--iMSF(即时消息服务框架)之【发布-订阅】模式
  16. httpd配置文件详解及实例
  17. kali安全工具
  18. jvm 启动参数设置(转载)
  19. SpringMVC(三)-- springmvc的系统学习之数据的处理,乱码及restful
  20. java之不修改变量的数据类型的处理方式

热门文章

  1. Mysql数据库基础知识
  2. openwrt的编译系统是如何生成squashfs文件系统的
  3. Java中线程出现Exception in thread "Thread-0" java.lang.IllegalMonitorStateException异常 解决方法
  4. HDU 1863 畅通工程 (最小生成树
  5. [bzoj 1260][CQOI 2007]涂色paint
  6. 关于使用jquery的Ajax结合java的Servlet后台判定用户名是否存在
  7. NS3 实验脚本的编写步骤
  8. HDU 6143 Killer Names(容斥原理)
  9. UVa 818 切断圆环链(dfs+二进制枚举)
  10. NOI 16 买房子