中断使用/.NET

问题描述 投票:0回答:1

我已经给出了下面的相关代码作为剧透。

我想创建一个表单来计算登录应用程序的人员每周和每月的总休息时间。

(GetWeeklyBreakUsage 和 GetMontlyBreakUsage)

此代码属于剧透的一部分:

WHERE USERS_ID = @UserId

在我的查询中,我写的是 299 而不是 @UserId。它在 MS-SQL 和应用程序 (.NET) 中都给出了正确的结果。但我想动态地获取它。凡是填写申请的人都应该休息一下。

其中 USERS_ID = 299

当我输入@UserId时 显示0小时0分0秒。没用。

我还从我的数据库上传了照片。

但它就是行不通。 我会再次提供更详细的信息。请帮忙。

*代码: *

>! {
public class StatisticsManager
>!     {

Haftalık Bağlanamayan Çağrıları Getir
public static int GetWeeklyMissedCalls(string loginName)
>!         {
int userId = GetUserIdByLoginName(loginName);  // LoginName ile USERS_ID'yi alıyoruz

string query = @"
SELECT COUNT(*) AS WeeklyMissedCalls
FROM [SesWareV7].[dbo].[cloud_MISSED_CALL_HISTORY]
WHERE USERS_ID = @UserId
AND START_TIME >= DATEADD(WEEK, -1, GETDATE())
AND CONNECT_STATUS = '0';";

return ExecuteScalarQuery(query, userId);
>!         }

Aylık Bağlanamayan Çağrıları Getir
public static int GetMonthlyMissedCalls(string loginName)
>!         {
int userId = GetUserIdByLoginName(loginName);  // LoginName ile USERS_ID'yi alıyoruz

string query = @"
SELECT COUNT(*) AS MonthlyMissedCalls
FROM [SesWareV7].[dbo].[cloud_MISSED_CALL_HISTORY]
WHERE USERS_ID = @UserId
AND START_TIME >= DATEADD(MONTH, -1, GETDATE())
AND CONNECT_STATUS = '0';";

return ExecuteScalarQuery(query, userId);
>!         }

public static string GetWeeklyBreakUsage(string loginName)
>!         {
int userId = GetUserIdByLoginName(loginName);  // LoginName ile USERS_ID'yi alıyoruz

string query = @"

SELECT SUM(DATEDIFF(SECOND, START_TIME, END_TIME)) AS WeeklyBreakUsage
FROM [SesWareV7].[dbo].[cloud_SUSPEND_HISTORY]
WHERE USERS_ID = @UserId
AND USERS_SUSPEND_STATUS_CODE IN (0, 1, 2, 3, 4)
AND START_TIME >= DATEADD(WEEK, -1, GETDATE());";

int totalDurationInSeconds = ExecuteScalarQuery(query, userId);

if (totalDurationInSeconds == 0)
return "0 saat 0 dakika 0 saniye";

Süreyi TimeSpan'e dönüştür
TimeSpan breakTimeSpan = TimeSpan.FromSeconds(totalDurationInSeconds);

Saat, dakika ve saniye olarak döndür
return $"{(int)breakTimeSpan.TotalHours} saat {breakTimeSpan.Minutes} dakika {breakTimeSpan.Seconds} saniye";
>!         }

public static string GetMonthlyBreakUsage(string loginName)
>!         {
int userId = GetUserIdByLoginName(loginName);  // LoginName ile USERS_ID'yi alıyoruz

string query = @"
SELECT SUM(DATEDIFF(SECOND, START_TIME, END_TIME)) AS MonthlyBreakUsage
FROM [SesWareV7].[dbo].[cloud_SUSPEND_HISTORY]
WHERE USERS_ID = @UserId
AND USERS_SUSPEND_STATUS_CODE IN (0, 1, 2, 3, 4)
AND START_TIME >= DATEADD(MONTH, -1, GETDATE());";

int totalDurationInSeconds = ExecuteScalarQuery(query, userId);

if (totalDurationInSeconds == 0)
return "0 saat 0 dakika 0 saniye";

Süreyi TimeSpan'e dönüştür
TimeSpan breakTimeSpan = TimeSpan.FromSeconds(totalDurationInSeconds);

Saat, dakika ve saniye olarak döndür
return $"{(int)breakTimeSpan.TotalHours} saat {breakTimeSpan.Minutes} dakika {breakTimeSpan.Seconds} saniye";
>!         }







public static int GetWeeklyAnsweredCalls(string loginName)
>!         {
string query = @"
SELECT COUNT(*) AS WeeklyAnsweredCalls
FROM [SesWareV7].[dbo].[cloud_USERS_CALL_HISTORY]
WHERE USERS_ID = (SELECT USERS_ID FROM [SesWareV7].[dbo].[cmm_USERS] WHERE LOGIN_NAME = @LoginName)
AND CONNECT_STATUS IN (0, 1)
AND CONVERT(DATE, START_TIME, 121) >= CONVERT(DATE, DATEADD(WEEK, -1, GETDATE()), 121);";

using (SqlConnection connection = new SqlConnection(connectionString))
>!             {
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@LoginName", loginName);

connection.Open();
var result = command.ExecuteScalar();
connection.Close();

return result != DBNull.Value && result != null ? Convert.ToInt32(result) : 0;
>!             }
>!         }
public static int GetMonthlyAnsweredCalls(string loginName)
>!         {
string query = @"
SELECT COUNT(*) AS MonthlyAnsweredCalls
FROM [SesWareV7].[dbo].[cloud_USERS_CALL_HISTORY]
WHERE USERS_ID = (SELECT USERS_ID FROM [SesWareV7].[dbo].[cmm_USERS] WHERE LOGIN_NAME = @LoginName)
AND CONNECT_STATUS IN (0, 1)
AND CONVERT(DATE, START_TIME, 121) >= CONVERT(DATE, DATEADD(MONTH, -1, GETDATE()), 121);";

using (SqlConnection connection = new SqlConnection(connectionString))
>!             {
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@LoginName", loginName);

connection.Open();
var result = command.ExecuteScalar();
connection.Close();

return result != DBNull.Value && result != null ? Convert.ToInt32(result) : 0;
>!             }
>!         }


public static int GetWeeklyRecordedCalls(string loginName)
>!         {
int userId = GetUserIdByLoginName(loginName);  // LoginName ile USERS_ID'yi alıyoruz

string query = @"
SELECT COUNT(*) AS WeeklyRecordedCalls
FROM [SesWareV7].[dbo].[cc_RECORDED_CALLS]
WHERE USERS_ID = @UserId
AND CONVERT(DATE, RECORD_DATE, 121) >= CONVERT(DATE, DATEADD(WEEK, -1, GETDATE()), 121);";

return ExecuteScalarQuery(query, userId);
>!         }

public static int GetMonthlyRecordedCalls(string loginName)
>!         {
int userId = GetUserIdByLoginName(loginName);  // LoginName ile USERS_ID'yi alıyoruz

string query = @"
SELECT COUNT(*) AS MonthlyRecordedCalls
FROM [SesWareV7].[dbo].[cc_RECORDED_CALLS]
WHERE USERS_ID = @UserId
AND CONVERT(DATE, RECORD_DATE, 121) >= CONVERT(DATE, DATEADD(MONTH, -1, GETDATE()), 121);";

return ExecuteScalarQuery(query, userId);
>!         }





USERS_ID'yi LoginName kullanarak getir
USERS_ID'yi cloud_SUSPEND_HISTORY tablosundan LoginName kullanarak getir
public static int GetUserIdByLoginName(string loginName)
>!         {
string query = @"
SELECT USERS_ID
FROM [SesWareV7].[dbo].[cmm_USERS]
WHERE LOGIN_NAME = @LoginName";

using (SqlConnection connection = new SqlConnection(connectionString))
>!             {
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@LoginName", loginName ?? (object)DBNull.Value); // LoginName parametresi ekle

connection.Open();
var result = command.ExecuteScalar();
connection.Close();

Sonuç varsa USERS_ID'yi döndür
return result != DBNull.Value && result != null ? Convert.ToInt32(result) : 0;
>!             }
>!         }




SQL sorgusunu çalıştır ve sonucu döndür
private static int ExecuteScalarQuery(string query, int userId)
>!         {
using (SqlConnection connection = new SqlConnection(connectionString))
>!             {
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@UserId", userId);

connection.Open();
var result = command.ExecuteScalar();
connection.Close();

Sonuç varsa döndür, yoksa 0 döndür
return result != DBNull.Value && result != null ? Convert.ToInt32(result) : 0;
>!             }
>!         }

*照片: * 在此输入图片描述

在此输入图片描述

写作299:

在此输入图片描述

在此输入图片描述

break
1个回答
0
投票

我写得很好。你能帮忙吗?

© www.soinside.com 2019 - 2024. All rights reserved.