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.
80 lines
2.4 KiB
80 lines
2.4 KiB
|
1 week ago
|
using CommunityToolkit.Mvvm.Input;
|
||
|
|
using Microsoft.Data.SqlClient;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
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 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;
|
||
|
|
|
||
|
|
_= 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)
|
||
|
|
{
|
||
|
|
Console.WriteLine("DataBase Link timeout\n"+ex.ToString());
|
||
|
|
// 失败处理
|
||
|
|
return; // 直接返回,不执行后续代码
|
||
|
|
}
|
||
|
|
Console.WriteLine("DataBase Link succeed");
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Database(string dat)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|