請用JavaScript來寫
时间: 2023-12-16 17:16:39 浏览: 20
好的,以下是使用 JavaScript 连接 MSSQL 数据库和显示排班表的示例代码:
```javascript
// 创建数据库连接
const sql = require('mssql');
const config = {
user: 'yourUserName',
password: 'yourPassword',
server: 'yourServerName',
database: 'yourDatabaseName',
options: {
encrypt: true
}
};
sql.connect(config).then(pool => {
// 编写SQL查询
const query = 'SELECT * FROM yourTableName WHERE yourCondition';
// 执行查询
return pool.request().query(query);
}).then(result => {
// 处理结果
const rows = result.recordset;
let table = document.createElement('table');
rows.forEach(row => {
let tr = document.createElement('tr');
let td1 = document.createElement('td');
let td2 = document.createElement('td');
td1.innerHTML = row.yourColumnName1;
td2.innerHTML = row.yourColumnName2;
tr.appendChild(td1);
tr.appendChild(td2);
table.appendChild(tr);
});
document.body.appendChild(table);
}).catch(err => {
// 处理错误
console.log(err);
});
```
请注意,上述代码中的“yourUserName”、“yourPassword”、“yourServerName”、“yourDatabaseName”、“yourTableName”和“yourColumnName1/2”需要替换为您自己的数据库连接信息和查询条件。此外,您需要使用 mssql 模块或其他适当的模块来连接 MSSQL 数据库。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)