gpt4 book ai didi

javascript - 如何使用 MSSQL 查询结果作为我的选择框选项

转载 作者:行者123 更新时间:2023-12-03 02:24:28 24 4
gpt4 key购买 nike

我有一个简单的 HTML Web 表单,其中包含一个选择框。我想设置选项以使用 MSSQL 查询的结果。 (这是产品编号列表)

这是我目前拥有的:

var {TYPES, Request, Connection} = require('tedious');

// Create connection to database
var config = {
userName: 'user',
password: '123123',
server: 'someserver',
options: {
instanceName: 'test'
}
};
var connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on('connect', (err) => {
if(err){
console.log(err);
}
run();
});
function run(request, result){
request = new Request("USE Database; SELECT PartNumber FROM dbo.Master ORDER BY PartNumber;", (err) =>{
if(err){
console.log(err);
connection.close();
}
connection.close();
});
var result = "";
request.on('row', (columns) =>{
columns.forEach((column) =>{
result+= column.value + " ";
});
console.log(result);
result = "";
});
connection.execSql(request);
}
<div class="row">
<div class="col span-1-of-3">
<label for="partnumber">PartNumber</label>
</div>
<div class="col span-2-of-3">
<select>
<option required></option>
</select>
</div>
</div>

我在网上找不到任何此类示例。我想单击选择框,然后 JavaScript 代码中的列表就会出现。我可以运行控制台并查看结果,但无法填充选项。

最佳答案

你可以尝试这样的事情。您需要循环结果并为每个项目创建选项。

var {TYPES, Request, Connection} = require('tedious');

// Create connection to database
var config = {
userName: 'user',
password: '123123',
server: 'someserver',
options: {
instanceName: 'test'
}
};
var connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on('connect', (err) => {
if(err){
console.log(err);
}
run();
});
function run(request, result){
request = new Request("USE Database; SELECT PartNumber FROM dbo.Master ORDER BY PartNumber;", (err) =>{
if(err){
console.log(err);
connection.close();
}
connection.close();
});
var result = "";
request.on('row', (columns) =>{
columns.forEach((column) =>{

var opt = document.createElement("option");
opt.value = column.value;
opt.innerHTML = column.value;
document.getElementById('results').appendChild(opt);


result+= column.value + " ";
});
console.log(result);
result = "";
});
connection.execSql(request);
}
<div class="row">
<div class="col span-1-of-3">
<label for="partnumber">PartNumber</label>
</div>
<div class="col span-2-of-3">
<select id="results">
<option required></option>
</select>
</div>
</div>

关于javascript - 如何使用 MSSQL 查询结果作为我的选择框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49011432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com