35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { Request } from 'mssql';
|
||
|
|
import { MssqlDBService } from 'src/db/db.service';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class MssqlService {
|
||
|
|
|
||
|
|
constructor(private readonly mssqlDBService: MssqlDBService) { }
|
||
|
|
|
||
|
|
async checkConnection() {
|
||
|
|
let connection;
|
||
|
|
|
||
|
|
try {
|
||
|
|
connection = await this.mssqlDBService.getConnection();
|
||
|
|
|
||
|
|
// Create a new Request object
|
||
|
|
const request = new Request(connection);
|
||
|
|
|
||
|
|
// Execute the query
|
||
|
|
const result = await request.query('SELECT GETDATE() AS CurrentDateTime');
|
||
|
|
|
||
|
|
console.log("Connection successful. Current date and time from the server:");
|
||
|
|
console.log(result.recordset[0].CurrentDateTime);
|
||
|
|
return { data: result.recordset[0].CurrentDateTime }
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error checking connection:', error);
|
||
|
|
} finally {
|
||
|
|
if (connection) {
|
||
|
|
connection.close(); // Ensure the connection is closed
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|