Nodejs使用tedious驱动模块连接SqlServer

app.js代码如下:

var Connection = require('tedious').Connection;
var mongoose = require('mongoose');
var rows = [];

//用户名,密码和数据库服务器,数据库
var config = {
  userName: 'sa',
  password: '123456',
  server: '127.0.0.1',
  options : {
             database  : 'BigDataTest'
            }
  };

  var connection = new Connection(config);
  var Request = require('tedious').Request;

  connection.on('connect', function(err) {
    //错误处理
    if(err)
	{
      console.log("连接出错!");
    }
    else {
      {
        console.log("连接成功!");
      };
    }
    getSqlData();
  });

     function getSqlData() 
	 {
        console.log('从SQL获取数据');
        request = new Request("SELECT * FROM Citys", function(err, rowCount)
        {
          if (err)
          {
            console.log(err);
          }
          else
          {
            console.log("行数: " + rowCount);
			//遍历对象数组,查出数据
			// rows.forEach(function(r){  
			// console.log(r["name"]+"---"+r["code"]);
		    // })  
          }
        });

        request.on('row', function(columns)
        {
          var row = {};
          columns.forEach(function(column)
          {
            row[column.metadata.colName] = column.value;
          });
          rows.push(row); 
		  
		});
 
        connection.execSql(request);
		
		
    }

sqlserver  建表脚本:

 
CREATE TABLE [dbo].[Citys](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[name] [nvarchar](100) NULL,
	[code] [varchar](300) NULL,
 CONSTRAINT [PK_Citys] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

结果如图:

Nodejs使用tedious驱动模块连接SqlServer


上一篇:Ubuntu18.04下配置深度学习开发环境


下一篇:struts-hibernate-ajax完成区县和街道级联下拉框功能(二补充使用json解析list结果集,ajax循环json层级处理)