[研究][ASP.NET][T-SQL]顯示 SQL Server 2019主機磁碟剩餘空間
2022-05-03
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
SQL Server Management Studio (SSMS) 中執行
DECLARE @SPACE AS TABLE (drive varchar(3), size decimal) insert into @SPACE EXEC master..xp_fixeddrives
SELECT serverproperty('ServerName') Server_Name,drive,size FROM @SPACE |
EXEC master..xp_fixeddrives |
(下圖)結果
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string queryString = @"EXEC master..xp_fixeddrives";
using (SqlConnection connection = new SqlConnection(
WebConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Label1.Text += reader["MB 可用"].ToString() + ",";
}
}
}
}
}
} |
執行結果
為什麼要研究這個?因為敝人管理的某系統 Web Server 和 Database Server 是不同主機,也在不同的網段,DB LAN 絕對禁止連 Internet,所以 SQL Server 本身就算有任何 Email 通知機制也沒用;所以想用 ASP.NET 網頁程式取得 DB 主機剩餘空間,檢測剩下多少 GB 時發 Email 通知。(完)



沒有留言:
張貼留言