2

我正在尝试运行 node-mssql 查询,如果我运行简单的查询,它将被执行。但是当我使用OPENROWSETand时Microsoft.ACE.OLEDB.12.0,它显示了一些错误。

这是server.js代码

var express = require('express');
var app = express();
app.get('/', function (req, res) {

var sql = require("mssql");

// config for your database
var config = {
    user: '..',
    password: '....',
    server: 'localhost\\SQLEXPRESS', 
    database: 'test_databasae' 
};

// connect to your database
sql.connect(config, function (err) {

    if (err)
         console.log(err);
     else
        console.log("Connection successful");


    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    /*request.query('select * from table1', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });*/
    request.query('INSERT INTO [mytable](SalesPersonID,TerritoryID)' + 
                    'SELECT SalesPersonID,TerritoryID FROM OPENROWSET('  + 
                    '\'Microsoft.ACE.OLEDB.12.0\',\'Excel 12.0\';\'Database=D:\\sample\\test\\data\\1540_OPENROWSET_Examples.xls;,\'' + 
                    'SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$])',function(err,recordset){
        if(err) console.log(err)

        console.log("success");
    }); 
});});var server = app.listen(5000, function () {
console.log('Server is running..');});`

当我在命令提示符下点击node server.js时,我在命令提示符下收到以下错误:

Server is running..
Connection successful
{ [RequestError: Incorrect syntax near ')'.]
  name: 'RequestError',
  message: 'Incorrect syntax near \')\'.',
  code: 'EREQUEST',
  number: 102,
  lineNumber: 1,
  state: 1,
  class: 15,
  serverName: 'localhost\\SQLEXPRESS',
  procName: '',
  precedingErrors:
   [ { [RequestError: Incorrect syntax near the keyword 'SELECT'.]
       name: 'RequestError',
       message: 'Incorrect syntax near the keyword \'SELECT\'.',
       code: 'EREQUEST',
       number: 156,
       lineNumber: 1,
       state: 1,
       class: 15,
       serverName: 'localhost\\SQLEXPRESS',
       procName: '' } ] }
success

在 SQL Server Management Studio 中执行相同的查询,它成功地将 excel 数据插入数据库。Excel 工作表数据如下所示:

SalesPersonID   TerritoryID
--------        -----------
275              2
276              4
277              3

这是plunker链接

4

1 回答 1

1

我在您的代码中看到一些语法错误:

示例代码为OPENROWSET

FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=D:\testing.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
  1. 在您的代码中额外'ExcelDatabase关键字之间,\'Excel 12.0\';\'Database=D:\\sample ...,需要更正

  2. 前后的SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$]需要。'

所以你的工作代码将是:

request.query('INSERT INTO [mytable](SalesPersonID, TerritoryID)' + 
                ' SELECT SalesPersonID, TerritoryID FROM OPENROWSET('  + 
                '\'Microsoft.ACE.OLEDB.12.0\', \'Excel 12.0;Database=D:\\sample\\test\\data\\1540_OPENROWSET_Examples.xls;HDR=YES\', ' + 
                '\'SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$]\')',function(err,recordset){
    if(err) console.log(err)

更新 :

OP 收到此配置错误:

链接服务器“(null)”的 OLE DB 提供程序“Microsoft.ACE.OLEDB.12.0”报告错误。

要修复配置错误,需要执行以下脚本:

USE [master] 
GO 

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 
GO 

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 
GO
于 2016-05-09T07:57:34.873 回答