{"id":81316,"date":"2024-10-16T15:18:18","date_gmt":"2024-10-16T07:18:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/81316\/"},"modified":"2024-10-16T15:18:18","modified_gmt":"2024-10-16T07:18:18","slug":"node-js%e8%88%87mssql%e9%85%8d%e5%90%88%e5%af%a6%e7%8f%be%e5%a4%a7%e6%89%b9%e9%87%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%ef%bc%88node-mssql%e6%89%b9%e9%87%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/81316\/","title":{"rendered":"Node.js\u8207MSSQL\u914d\u5408\u5be6\u73fe\u5927\u6279\u91cf\u6578\u64da\u5eab\u64cd\u4f5c\uff08Node MSSQL\u6279\u91cf\uff09"},"content":{"rendered":"<h1 id=\"node-js%e8%88%87mssql%e9%85%8d%e5%90%88%e5%af%a6%e7%8f%be%e5%a4%a7%e6%89%b9%e9%87%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%ef%bc%88node-mssql%e6%89%b9%e9%87%8f%ef%bc%89-uagryRaeKz\">Node.js\u8207MSSQL\u914d\u5408\u5be6\u73fe\u5927\u6279\u91cf\u6578\u64da\u5eab\u64cd\u4f5c\uff08Node MSSQL\u6279\u91cf\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0cNode.js\u56e0\u5176\u9ad8\u6548\u7684\u975e\u963b\u585eI\/O\u6a21\u578b\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u7576\u9700\u8981\u8207\u6578\u64da\u5eab\u9032\u884c\u5927\u6279\u91cf\u64cd\u4f5c\u6642\uff0cNode.js\u8207Microsoft SQL Server\uff08MSSQL\uff09\u7684\u7d50\u5408\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Node.js\u8207MSSQL\u9032\u884c\u5927\u6279\u91cf\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"node-js%e8%88%87mssql%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-uagryRaeKz\">Node.js\u8207MSSQL\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Node.js\u662f\u4e00\u500b\u57fa\u65bcChrome V8\u5f15\u64ce\u7684JavaScript\u904b\u884c\u74b0\u5883\uff0c\u9069\u5408\u7528\u65bc\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u3002\u800cMSSQL\u5247\u662f\u5fae\u8edf\u7684\u4e00\u6b3e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u4f01\u696d\u7d1a\u61c9\u7528\u4e2d\u3002\u5169\u8005\u7684\u7d50\u5408\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5229\u7528JavaScript\u7684\u9748\u6d3b\u6027\u548cMSSQL\u7684\u5f37\u5927\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c%ef%bc%9f-uagryRaeKz\">\u70ba\u4ec0\u9ebc\u9078\u64c7\u6279\u91cf\u64cd\u4f5c\uff1f<\/h2>\n<p>\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u9010\u689d\u63d2\u5165\u6216\u66f4\u65b0\u6578\u64da\u6703\u5c0e\u81f4\u6027\u80fd\u74f6\u9838\u3002\u6279\u91cf\u64cd\u4f5c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u6e1b\u5c11\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u6b21\u6578\uff0c\u5f9e\u800c\u964d\u4f4e\u5ef6\u9072\u548c\u8cc7\u6e90\u6d88\u8017\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d%e5%bf%85%e8%a6%81%e7%9a%84%e5%ba%ab-uagryRaeKz\">\u5b89\u88dd\u5fc5\u8981\u7684\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5b89\u88ddNode.js\u548c\u76f8\u61c9\u7684MSSQL\u5eab\u3002\u53ef\u4ee5\u4f7f\u7528npm\u4f86\u5b89\u88ddmssql\u5eab\uff0c\u9019\u662f\u4e00\u500b\u6d41\u884c\u7684Node.js MSSQL\u5ba2\u6236\u7aef\u3002<\/p>\n<pre><code>npm install mssql<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0mssql%e6%95%b8%e6%93%9a%e5%ba%ab-uagryRaeKz\">\u9023\u63a5\u5230MSSQL\u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u8a2d\u7f6e\u8207MSSQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u9023\u63a5\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code>const sql = require('mssql');\n\nconst config = {\n    user: 'your_username',\n    password: 'your_password',\n    server: 'your_server', \n    database: 'your_database',\n    options: {\n        encrypt: true \/\/ \u4f7f\u7528\u52a0\u5bc6\u9023\u63a5\n    }\n};\n\nsql.connect(config).then(pool =&gt; {\n    console.log('Connected to MSSQL');\n}).catch(err =&gt; {\n    console.error('Database connection failed:', err);\n});<\/code><\/pre>\n<h2 id=\"%e5%af%a6%e7%8f%be%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%93%8d%e4%bd%9c-uagryRaeKz\">\u5be6\u73fe\u6279\u91cf\u63d2\u5165\u64cd\u4f5c<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Node.js\u9032\u884c\u6279\u91cf\u63d2\u5165\u64cd\u4f5c\uff1a<\/p>\n<pre><code>async function bulkInsert(data) {\n    try {\n        const pool = await sql.connect(config);\n        const table = new sql.Table('YourTableName');\n        table.create = false; \/\/ \u5982\u679c\u8868\u5df2\u5b58\u5728\uff0c\u5247\u8a2d\u7f6e\u70bafalse\n        table.columns.add('Column1', sql.Int, { nullable: false });\n        table.columns.add('Column2', sql.NVarChar(50), { nullable: false });\n\n        data.forEach(item =&gt; {\n            table.rows.add(item.column1, item.column2);\n        });\n\n        const request = pool.request();\n        await request.bulk(table);\n        console.log('Bulk insert successful');\n    } catch (err) {\n        console.error('Bulk insert failed:', err);\n    }\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\u6578\u64da\u9032\u884c\u6279\u91cf\u63d2\u5165\nconst sampleData = [\n    { column1: 1, column2: 'Data1' },\n    { column1: 2, column2: 'Data2' },\n    \/\/ \u66f4\u591a\u6578\u64da...\n];\n\nbulkInsert(sampleData);<\/code><\/pre>\n<h2 id=\"%e8%99%95%e7%90%86%e6%89%b9%e9%87%8f%e6%9b%b4%e6%96%b0%e6%93%8d%e4%bd%9c-uagryRaeKz\">\u8655\u7406\u6279\u91cf\u66f4\u65b0\u64cd\u4f5c<\/h2>\n<p>\u9664\u4e86\u63d2\u5165\uff0c\u60a8\u9084\u53ef\u4ee5\u4f7f\u7528\u985e\u4f3c\u7684\u65b9\u6cd5\u9032\u884c\u6279\u91cf\u66f4\u65b0\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>async function bulkUpdate(data) {\n    try {\n        const pool = await sql.connect(config);\n        const table = new sql.Table('YourTableName');\n        table.create = false;\n        table.columns.add('Column1', sql.Int, { nullable: false });\n        table.columns.add('Column2', sql.NVarChar(50), { nullable: false });\n\n        data.forEach(item =&gt; {\n            table.rows.add(item.column1, item.column2);\n        });\n\n        const request = pool.request();\n        await request.bulk(table);\n        console.log('Bulk update successful');\n    } catch (err) {\n        console.error('Bulk update failed:', err);\n    }\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\u6578\u64da\u9032\u884c\u6279\u91cf\u66f4\u65b0\nconst updateData = [\n    { column1: 1, column2: 'UpdatedData1' },\n    { column1: 2, column2: 'UpdatedData2' },\n    \/\/ \u66f4\u591a\u6578\u64da...\n];\n\nbulkUpdate(updateData);<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uagryRaeKz\">\u7e3d\u7d50<\/h2>\n<p>Node.js\u8207MSSQL\u7684\u7d50\u5408\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u8655\u7406\u5927\u6279\u91cf\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u901a\u904e\u4f7f\u7528\u6279\u91cf\u63d2\u5165\u548c\u66f4\u65b0\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6027\u80fd\uff0c\u5f9e\u800c\u63d0\u5347\u61c9\u7528\u7684\u6574\u9ad4\u6548\u7387\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u74b0\u5883\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Node.js\u8207MSSQL\u9ad8\u6548\u5be6\u73fe\u5927\u6279\u91cf\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u5347\u6027\u80fd\u8207\u8655\u7406\u901f\u5ea6\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-81316","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=81316"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81316\/revisions"}],"predecessor-version":[{"id":81317,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81316\/revisions\/81317"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=81316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=81316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=81316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}