You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.7 KiB
174 lines
5.7 KiB
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Data.SqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Text;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using TouchSocket.Sockets;
|
|
|
|
namespace SunlightAggregationManager.UserClass
|
|
{
|
|
public class DataBase
|
|
{
|
|
private string DB_NAME = null!;
|
|
private string DB_TYPE = null!;
|
|
private string DB_ID = null!;
|
|
private string DB_IP = null!;
|
|
private string DB_PASSWORD = null!;
|
|
|
|
public async void Config(string ip, string name, string type, string id, string password)
|
|
{
|
|
DB_IP = ip;
|
|
DB_ID = id;
|
|
DB_NAME = name;
|
|
DB_TYPE = type;
|
|
DB_PASSWORD = password;
|
|
|
|
await ConfigAsync();
|
|
|
|
}
|
|
public async Task ConfigAsync()
|
|
{
|
|
await Task.Delay(3000);
|
|
//进入后台线程操作
|
|
await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
// 构建连接字符串
|
|
var builder = new SqlConnectionStringBuilder
|
|
{
|
|
DataSource = DB_IP,
|
|
InitialCatalog = DB_NAME,
|
|
IntegratedSecurity = DB_TYPE == "Windows Authentication",
|
|
TrustServerCertificate = true,
|
|
ConnectTimeout = 5
|
|
};
|
|
|
|
if (!builder.IntegratedSecurity)
|
|
{
|
|
builder.UserID = DB_ID;
|
|
builder.Password = DB_PASSWORD;
|
|
}
|
|
|
|
//连接数据库
|
|
using var conn_SC = new SqlConnection(builder.ConnectionString);
|
|
conn_SC.Open();
|
|
using (var cmd = new SqlCommand("SELECT 1", conn_SC))
|
|
{
|
|
cmd.ExecuteReader();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogGing.ERRDATA(ex);
|
|
Console.WriteLine("DataBase Link timeout\n"+ex.ToString());
|
|
// 失败处理
|
|
return; // 直接返回,不执行后续代码
|
|
}
|
|
Console.WriteLine("DataBase Link succeed");
|
|
});
|
|
}
|
|
|
|
public async Task<DataTable> ExecuteQueryAsync(string sql)
|
|
{
|
|
// 创建一个空的 DataTable 用于存放查询结果
|
|
DataTable dataTable = new DataTable();
|
|
|
|
// 数据库连接字符串构建逻辑
|
|
var builder = new SqlConnectionStringBuilder
|
|
{
|
|
DataSource = DB_IP,
|
|
InitialCatalog = DB_NAME,
|
|
IntegratedSecurity = DB_TYPE == "Windows Authentication",
|
|
TrustServerCertificate = true,
|
|
ConnectTimeout = 5
|
|
};
|
|
|
|
if (!builder.IntegratedSecurity)
|
|
{
|
|
builder.UserID = DB_ID;
|
|
builder.Password = DB_PASSWORD;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var conn = new SqlConnection(builder.ConnectionString);
|
|
await conn.OpenAsync(); // 使用异步方式打开连接<websource>source_group_web_2</websource>
|
|
|
|
using (var cmd = new SqlCommand(sql, conn))
|
|
{
|
|
// 执行查询并将结果直接加载到 DataTable 中
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
dataTable.Load(reader);
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogGing.ERRDATA(ex);
|
|
Console.WriteLine("DataBase Query failed\n" + ex.ToString());
|
|
// 将异常抛出给调用方,或者返回一个空的 DataTable
|
|
throw;
|
|
}
|
|
|
|
return dataTable;
|
|
}
|
|
|
|
public async Task<int> ExecuteQueryAsyncCount(string sql)
|
|
{
|
|
// 创建一个空的 DataTable 用于存放查询结果
|
|
DataTable dataTable = new DataTable();
|
|
|
|
// 数据库连接字符串构建逻辑
|
|
var builder = new SqlConnectionStringBuilder
|
|
{
|
|
DataSource = DB_IP,
|
|
InitialCatalog = DB_NAME,
|
|
IntegratedSecurity = DB_TYPE == "Windows Authentication",
|
|
TrustServerCertificate = true,
|
|
ConnectTimeout = 5
|
|
};
|
|
|
|
if (!builder.IntegratedSecurity)
|
|
{
|
|
builder.UserID = DB_ID;
|
|
builder.Password = DB_PASSWORD;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var conn = new SqlConnection(builder.ConnectionString);
|
|
await conn.OpenAsync(); // 使用异步方式打开连接<websource>source_group_web_2</websource>
|
|
|
|
using (var cmd = new SqlCommand(sql, conn))
|
|
{
|
|
// 执行查询并将结果直接加载到 DataTable 中
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
dataTable.Load(reader);
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogGing.ERRDATA(ex);
|
|
Console.WriteLine("DataBase Query failed\n" + ex.ToString());
|
|
// 将异常抛出给调用方,或者返回一个空的 DataTable
|
|
throw;
|
|
}
|
|
|
|
return dataTable.Rows.Count;
|
|
}
|
|
|
|
public void Database(string dat)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|