Files
QQBot/Mysql.cs
MelodyMaster 5ca29d24d6 error
2025-06-12 11:28:05 +08:00

90 lines
2.9 KiB
C#

using MySql.Data.MySqlClient;
using static System.Net.Mime.MediaTypeNames;
namespace QQBot.SQL
{
public class Mysql
{
private static readonly string ConnectionString = "server=8.138.193.132;port=3306;user=slroot;password=root;database=sl;charset=utf8mb4;";
// 添加 IsSql 字段
public static bool IsSql { get; private set; }
private static MySqlConnection Connection { get; set; }
// 验证绑定码是否存在
public static async Task InitSql()
{
Connection = new MySqlConnection(ConnectionString);
try
{
IsSql = true;
Connection.Open();
Connection.Close();
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
IsSql = false;
}
}
public static BindResult ValidateBindCode(string code, string qqNumber)
{
var result = new BindResult();
using (var conn = new MySqlConnection(ConnectionString))
{
try
{
conn.Open();
var cmd = new MySqlCommand(
"SELECT uid, steam17id FROM binding WHERE bind_code = @code AND qq_number IS NULL",
conn);
cmd.Parameters.AddWithValue("@code", code);
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
result.Success = true;
result.Uid = reader["uid"].ToString();
result.SteamId = reader["steam17id"].ToString();
}
else
{
result.Message = "无效或已被使用的绑定码";
}
}
}
catch (Exception ex)
{
result.Message = $"数据库错误: {ex.Message}";
}
}
return result;
}
// 更新绑定码对应的QQ号
public static bool UpdateQQNumber(string code, string qq)
{
using (var conn = new MySqlConnection(ConnectionString))
{
conn.Open();
var cmd = new MySqlCommand("UPDATE binding SET qq_number = @qq WHERE bind_code = @code", conn);
cmd.Parameters.AddWithValue("@qq", qq);
cmd.Parameters.AddWithValue("@code", code);
return cmd.ExecuteNonQuery() > 0;
}
}
}
public class BindResult
{
public bool Success { get; set; }
public string Message { get; set; }
public string Uid { get; set; }
public string SteamId { get; set; }
}
}