欢迎访问宙启技术站
智能推送

nodejs基于mssql模块连接sqlserver数据库的示例分析

发布时间:2023-05-16 19:39:49

Node.js是一种非常流行的JavaScript运行时环境,允许前端开发人员使用JavaScript编写服务器端应用程序。MSSQL模块是Node.js中一个重要的模块,该模块提供了与Microsoft SQL Server数据库的通信功能。本文将介绍如何使用MSSQL模块连接SQLServer数据库,并简要介绍在Node.js中的MSSQL模块的一些常用功能。

一、使用MSSQL模块连接SQLServer数据库

在使用MSSQL模块之前,首先需要安装MSSQL模块。通过npm安装MSSQL模块的方法如下:

npm install mssql

安装完MSSQL模块后,就可以开始使用它来连接SQLServer数据库。MSSQL模块提供了许多选项来配置连接参数。例如,可以配置服务器名称、数据库名称、用户名、密码等。在下面的示例中,我们将演示如何通过MSSQL模块连接SQLServer数据库。

const sql = require('mssql');

const config = {

    server: 'localhost',

    database: 'Test',

    user: 'sa',

    password: 'admin123',

    port: 1433

};

sql.connect(config, function (err) {

    if (err) {

        console.log(err);

        return;

    }

    console.log('SQL Server连接成功');

});

在这个例子中,我们通过给config对象传递所有连接参数来构建一个配置对象。将config对象传递给sql.connect方法来连接SQLServer数据库。当连接成功时,会在控制台上输出一条消息。如果连接失败,则会输出错误消息。

二、MSSQL模块的常用功能

MSSQL模块提供了许多功能来访问和操作SQLServer数据库。下面介绍几个最常用的功能。

1. 查询数据

MSSQL模块提供了query方法来查询数据。下面是一个简单的查询示例:

const sql = require('mssql');

const config = {

    server: 'localhost',

    database: 'Test',

    user: 'sa',

    password: 'admin123',

    port: 1433

};

sql.connect(config, function (err) {

    if (err) {

        console.log(err);

        return;

    }

    new sql.Request().query('SELECT * FROM Employee', function (err, result) {

        if (err) {

            console.log(err);

            return;

        }

        console.log(result);

    });

});

在这个例子中,我们通过new sql.Request().query方法发出查询请求。要查询的内容是在字符串参数中的SQL语句。查询结果通过回调函数返回。

2. 插入数据

MSSQL模块提供了一个方便的方法来插入数据到数据库中。下面是一个简单的示例:

const sql = require('mssql');

const config = {

    server: 'localhost',

    database: 'Test',

    user: 'sa',

    password: 'admin123',

    port: 1433

};

sql.connect(config, function (err) {

    if (err) {

        console.log(err);

        return;

    }

    let employee = {

        FirstName: 'John',

        LastName: 'Doe',

        Title: 'Software Engineer',

        HireDate: new Date(),

        Salary: 80000

    };

    new sql.Request().input('FirstName', sql.NVarChar, employee.FirstName)

        .input('LastName', sql.NVarChar, employee.LastName)

        .input('Title', sql.NVarChar, employee.Title)

        .input('HireDate', sql.DateTime, employee.HireDate)

        .input('Salary', sql.Numeric, employee.Salary)

        .query('INSERT INTO Employee (FirstName, LastName, Title, HireDate, Salary) VALUES (@FirstName, @LastName, @Title, @HireDate, @Salary)', function (err, result) {

            if (err) {

                console.log(err);

                return;

            }

            console.log(result);

        });

});

在这个例子中,我们创建了一个employee对象,然后将其插入数据库中。我们通过new sql.Request().input方法将employee对象中的每个属性添加到查询中。

3. 更新数据

MSSQL模块提供了一个方便的方法来更新数据库中的数据。下面是一个简单的示例:

const sql = require('mssql');

const config = {

    server: 'localhost',

    database: 'Test',

    user: 'sa',

    password: 'admin123',

    port: 1433

};

sql.connect(config, function (err) {

    if (err) {

        console.log(err);

        return;

    }

    new sql.Request().input('Salary', sql.Numeric, 90000)

        .query('UPDATE Employee SET Salary = @Salary WHERE FirstName = "John" AND LastName = "Doe"', function (err, result) {

            if (err) {

                console.log(err);

                return;

            }

            console.log(result);

        });

});

在这个例子中,我们通过new sql.Request().input方法将要更新的数据传递了一个新的值。注意,SQL语句中使用等号(=)而不是冒号(:)设置参数。

4. 删除数据

MSSQL模块提供了一个方便的方法来删除数据库中的数据。下面是一个简单的示例:

const sql = require('mssql');

const config = {

    server: 'localhost',

    database: 'Test',

    user: 'sa',

    password: 'admin123',

    port: 1433

};

sql.connect(config, function (err) {

    if (err) {

        console.log(err);

        return;

    }

    new sql.Request().query('DELETE FROM Employee WHERE LastName = "Doe"', function (err, result) {

        if (err) {

            console.log(err);

            return;

        }

        console.log(result);

    });

});

在这个例子中,我们通过new sql.Request().query方法来执行删除操作。注意,SQL语句中使用双引号(“)而不是单引号(')包含字符串。

总结:

本文介绍了如何使用MSSQL模块连接SQLServer数据库,并简要介绍了在Node.js中MSSQL模块的一些常用功能。使用MSSQL模块,可以轻松地连接SQLServer数据库并对其进行访问和操作。