直接上代码:

  1. /**
    * Created by chaozhou on 2015/9/18.
    */
    var mssql = require('mssql');
    var user = "sa",
    password = "sa",
    server = "192.168.20.132",
    database = "ggcms";
  2.  
  3. /**
    * 默认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
    }
    };
  4.  
  5. /**
    * 初始化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
    }
    }
    };
  6.  
  7. /**
    * 恢复默认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
    }
    };
    };
  8.  
  9. /**
    * 执行原生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);
    }
    }
    }
    console.log("sql:" + sql);
    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();
    };
  10.  
  11. /**
    * 带参数查询
    * @param tableName 表名
    * @param topNumber 前topNumber条
    * @param whereSql whereSql
    * @param params 查询参数对象(可为"",为""表示不加任何参数,如果此项为"",则whereSql必须也为"")
    * @param orderSql 排序Sql(可为"",为""表示不排序)
    * @param callBack
    */
    var select = function (tableName, topNumber, whereSql, params, orderSql, callBack) {
    var connection = new mssql.Connection(config, function (err) {
    var ps = new mssql.PreparedStatement(connection);
    var sql = "select * from " + tableName + " ";
    if (topNumber != "") {
    sql = "select top(" + topNumber + ") * from " + tableName + " ";
    }
    sql += whereSql + " ";
    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);
    }
    }
    }
    sql += orderSql;
    console.log(sql);
    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();
    };
  12.  
  13. //select("dbo.userInfo",3,"where id = @id",{id:1},"order by id",function(err,recordset){
    // console.log(recordset);
    //});
  14.  
  15. /**
    * 查询所有
    * @param tableName
    * @param callBack
    */
    var selectAll = function (tableName, callBack) {
    var connection = new mssql.Connection(config, function (err) {
    var ps = new mssql.PreparedStatement(connection);
    var sql = "select * from " + tableName + " ";
    console.log("sql:" + sql);
    ps.prepare(sql, function (err) {
    if (err)
    console.log(err);
    ps.execute("", function (err, recordset) {
    callBack(err, recordset);
    ps.unprepare(function (err) {
    if (err)
    console.log(err);
    });
    });
    });
    });
    restoreDefaults();
    };
  16.  
  17. //selectAll("dbo.userTable",function(err,recordset){
    // console.log(recordset);
    //});
  18.  
  19. /**
    * 添加
    * @param addObj 添加对象(必填)
    * @param tableName 表名
    * @param callBack(err,recordset)
    */
    var add = function(addObj,tableName,callBack){ //{id:3,userName:'admin'...} insert into dbo.tags(id,name) values(@id,@name)
    var connection = new mssql.Connection(config, function (err) {
    var ps = new mssql.PreparedStatement(connection);
    var sql = "insert into " + tableName + "(";
    if (addObj != "") {
    for (var index in addObj) {
    if (typeof addObj[index] == "number") {
    ps.input(index, mssql.Int);
    } else if (typeof addObj[index] == "string") {
    ps.input(index, mssql.NVarChar);
    } else if (typeof addObj[index] == "object") {
    ps.input(index, mssql.DateTime);
    }
    sql += index + ",";
    }
    sql = sql.substr(0, sql.length - 1) + ")values(";
    for (var index in addObj) {
    sql = sql + "@" + index + ",";
    }
    }
    sql = sql.substr(0, sql.length - 1) + ")";
    console.log(sql);
    ps.prepare(sql, function (err) {
    if (err)
    console.log(err);
    ps.execute(addObj, function (err, recordset) {
    callBack(err, recordset);
    ps.unprepare(function (err) {
    if (err)
    console.log(err);
    });
    });
    });
    });
    restoreDefaults();
    };
  20.  
  21. //add({"updateTime":new Date(),name:'awdaw,3awdwa,3434'},"dbo.template",function(err,recordset){
    // console.log(recordset);
    //});
  22.  
  23. //var add = function (addObj, tableName, callBack) {
    // var connection = new mssql.Connection(config, function (err) {
    // var ps = new mssql.PreparedStatement(connection);
    // var sql = "insert into " + tableName + "(";
    // if (addObj != "") {
    // for (var index in addObj) {
    // if (typeof addObj[index] == "number") {
    // ps.input(index, mssql.Int);
    // } else if (typeof addObj[index] == "string") {
    // ps.input(index, mssql.NVarChar);
    // }
    // sql += index + ",";
    // }
    // sql = sql.substring(0, sql.length - 1) + ") values(";
    // for (var index in addObj) {
    // if (typeof addObj[index] == "number") {
    // sql += addObj[index] + ",";
    // } else if (typeof addObj[index] == "string") {
    // sql += "'" + addObj[index] + "'" + ",";
    // }
    // }
    // }
    // sql = sql.substring(0, sql.length - 1) + ")";
    // console.log("sql:" + sql);
    // ps.prepare(sql, function (err) {
    // if (err)
    // console.log(err);
    // ps.execute(addObj, function (err, recordset) {
    // callBack(err, recordset);
    // ps.unprepare(function (err) { //回收连接至连接池
    // if (err)
    // console.log(err);
    // });
    // });
    // });
    // });
    // restoreDefaults();
    //};
  24.  
  25. /**
    * 修改
    * @param updateObj 修改内容(必填)
    * @param whereObj 修改对象(必填)
    * @param tableName 表名
    * @param callBack(err,recordset)
    */
    var update = function(updateObj, whereObj, tableName, callBack){
    var connection = new mssql.Connection(config, function (err) {
    var ps = new mssql.PreparedStatement(connection);
    var sql = "update " + tableName + " set ";
    if (updateObj != "") {
    for (var index in updateObj) {
    if (typeof updateObj[index] == "number") {
    ps.input(index, mssql.Int);
    } else if (typeof updateObj[index] == "string") {
    ps.input(index, mssql.NVarChar);
    } else if (typeof updateObj[index] == "object") {
    ps.input(index, mssql.DateTime);
    }
    sql += index + "=@" + index + ",";
    }
    sql = sql.substr(0, sql.length - 1) + " where ";
    }
    if (whereObj != "") {
    for (var index in whereObj) {
    if (typeof whereObj[index] == "number") {
    ps.input(index, mssql.Int);
    } else if (typeof whereObj[index] == "string") {
    ps.input(index, mssql.NVarChar);
    } else if (typeof whereObj[index] == "object") {
    ps.input(index, mssql.DateTime);
    }
    sql += index + "=@" + index + ",";
    }
    }
    sql = sql.substr(0, sql.length - 1);
    var whereStr = JSON.stringify(whereObj);
    var updateStr = JSON.stringify(updateObj);
    whereObj = JSON.parse(updateStr.substr(0,updateStr.length -1) + "," + whereStr.substr(1,whereStr.length));
    console.log(sql);
    ps.prepare(sql, function (err) {
    if (err)
    console.log(err);
    ps.execute(whereObj, function (err, recordset) {
    callBack(err, recordset);
    ps.unprepare(function (err) {
    if (err)
    console.log(err);
    });
    });
    });
    });
    restoreDefaults();
    };
  26.  
  27. //update({name:"awdawd",context:'awdaw33434',updateTime:'2015-09-25'},{id:2},"dbo.template",function(err,recordset){
    // console.log(recordset);
    //});
  28.  
  29. //var update = function (updateObj, whereObj, tableName, callBack) {
    // var connection = new mssql.Connection(config, function (err) {
    // var ps = new mssql.PreparedStatement(connection);
    // var sql = "update " + tableName + " set "; //update userTable set userName = 'admin',loginTimes = 12,password = 'admin'
    // if (updateObj != "") {
    // for (var index in updateObj) {
    // if (typeof updateObj[index] == "number") {
    // ps.input(index, mssql.Int);
    // sql += index + "=" + updateObj[index] + ",";
    // } else if (typeof updateObj[index] == "string") {
    // ps.input(index, mssql.NVarChar);
    // sql += index + "=" + "'" + updateObj[index] + "'" + ",";
    // }
    // }
    // }
    // sql = sql.substring(0, sql.length - 1) + " where ";
    // if (whereObj != "") {
    // for (var index in whereObj) {
    // if (typeof whereObj[index] == "number") {
    // ps.input(index, mssql.Int);
    // sql += index + "=" + whereObj[index] + " and ";
    // } else if (typeof whereObj[index] == "string") {
    // ps.input(index, mssql.NVarChar);
    // sql += index + "=" + "'" + whereObj[index] + "'" + " and ";
    // }
    // }
    // }
    // sql = sql.substring(0, sql.length - 5);
    // console.log("sql:" + sql);
    // ps.prepare(sql, function (err) {
    // if (err)
    // console.log(err);
    // ps.execute(updateObj, function (err, recordset) {
    // callBack(err, recordset);
    // ps.unprepare(function (err) { //回收连接至连接池
    // if (err)
    // console.log(err);
    // });
    // });
    // });
    // });
    // restoreDefaults();
    //};
  30.  
  31. /**
    * 删除
    * @param deleteObj 删除对象
    * @param tableName 表名
    * @param callBack(err,recordset)
    */
    var del = function (whereSql, params, tableName, callBack) {
    var connection = new mssql.Connection(config, function (err) {
    var ps = new mssql.PreparedStatement(connection);
    var sql = "delete from " + tableName + " ";
    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);
    }
    }
    }
    sql += whereSql;
    console.log("sql:" + sql);
    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();
    };
  32.  
  33. //del("where id = @id",{id:16},"dbo.userTable",function(err,recordset){
    // console.log(recordset);
    //});
  34.  
  35. exports.initConfig = initConfig;
    exports.config = config;
    exports.del = del;
    exports.select = select;
    exports.update = update;
    exports.querySql = querySql;
    exports.restoreDefaults = restoreDefaults;
    exports.selectAll = selectAll;
    exports.add = add;

Express调用mssql驱动公共类dbHelper的更多相关文章

  1. C#.NET数据库访问类DBHelper

    这是一个与C# .NET通用的数据库访问类,包含了工厂模式.事务处理等安全机制. 调用方式: DBHelper db = new DBHelper(); DbCommand cmd = db.GetS ...

  2. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  3. Mybatis包分页查询java公共类

    Mybatis包分页查询java公共类   分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...

  4. CodeSmith公共类维护

    CodeSmith在使用过程中,我们经常会出现同一个方法在不同的页面调用,如果我们在每个页面都写一个这样的方法,那么代码量非常大,同时如果以后需要修改也要在每个页面分别去修改,这无疑是劳命伤财,如果能 ...

  5. C#通过SC命令和静态公共类来操作Windows服务

    调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...

  6. Laravel驱动管理类Manager的分析和使用

    Laravel驱动管理类Manager的分析和使用 第一部分 概念说明 第二部分 Illuminate\Support\Manager源码 第三部分 Manager类的使用 第一部分:概念解释 结合实 ...

  7. Android驱动学习-app调用内核驱动过程(驱动框架回顾)

    考研已经过去了,android驱动的学习也断了半年多了,现在重新捡起来学习,回顾一下Android驱动的大体框架. Android系统的核心是java,其有一个David虚拟机.Android-app ...

  8. 一个Java文件至多包含一个公共类

    编写一个java源文件时,该源文件又称为编译单元.一个java文件可以包含多个类,但至多包含一个公共类,作为编译时该java文件的公用接口,公共类的名字和源文件的名字要相同,源文件名字的格式为[公共类 ...

  9. WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用

    WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用

随机推荐

  1. linux ssh config

    Host code.engineering.redhat.com    HostName code.engineering.redhat.com    Port 29418    User jiall ...

  2. iptables 四表五链

    netfilter/iptables IP 信息包过滤系统是一种功能强大的工具,可用于添加.编辑和除去规则,这些规则是在做信息包过滤决定时,防火墙所遵循和组成的规则.这些规则存储在专用的信息包过滤表中 ...

  3. 0002--Weekly Meeting on 27th March and 3th April, 2015

    27th March, 2015 (1) RankNet && PageRank ->reporter: jinquan Du   Web_RankNet  Web_PageRa ...

  4. Xamarin开发Android笔记:使用ZXing进行连续扫描

    在项目开发中需要使用到条码扫描,因为以前就测试过ZXing,感觉识别速度和功能都不错,所以直接引用.不过在实际开发的过程中,却遇到连续扫描的问题,每次扫描识别完成之后,扫描窗体自动关闭了. 在Xama ...

  5. MongoDB之TextSearch简介

    MongoDB之TextSearch简介  MongoDB支持对文本内容执行文本搜索操作,其提供了索引text index和查询操作$text来完成文本搜索功能.下面我们通过一个简单的例子来体验一下M ...

  6. 使用curl 下载HTML

    简单的一个curl小例子: #include <iostream> #include <string> #include <sstream> #include &l ...

  7. Python自动化测试(1)-自动化测试及基本技术手段概述

    生产力概述 在如今以google为首的互联网时代,软件的开发和生产模式都已经发生了变化, 在<参与感>一书提到:某位从微软出来的工程师很困惑,微软在google还有facebook这些公司 ...

  8. JavaScript函数编程-Ramdajs

    在JavaScript语言世界,函数是第一等公民.JavaScript函数是继承自Function的对象,函数能作另一个函数的参数或者返回值使用,这便形成了我们常说的高阶函数(或称函数对象).这就构成 ...

  9. 一天一小段js代码(no.4)

    最近在看网上的前端笔试题,借鉴别人的自己来试一下: 题目: 写一段脚本,实现:当页面上任意一个链接被点击的时候,alert出这个链接在页面上的顺序号,如第一个链接则alert(1), 依次类推. 有一 ...

  10. Ubuntu环境搭建系列—Chrome/JDK/Android篇

    其实每次重装Ubuntu系统的时候都要进行一次基本到环境配置,而且每次总会忘记一些环境配置到东西,所以就写下这个博文,方便自己以后重装系统的时候回顾,同时也给大家做为重装系统后基本环境搭建的参考. 因 ...