我正在尝试使用SQL创建一个插件,但是我遇到了一些我不知道如何处理它们的错误。 SQL数据库管理器+从主类添加播放器。
==
其他代码:
private void Events_OnPlayerConnected(UnturnedPlayer player)
{
FriendsGroupID_t id;
id.m_FriendsGroupID = (short)player.SteamGroupID.m_SteamID;
Database.addPlayer(player.CharacterName, player.SteamName, SteamFriends.GetFriendsGroupName(id), player.CSteamID.m_SteamID.ToString());
}
这是我在调用函数using System;
using I18N.West;
using MySql.Data.MySqlClient;
using Rocket.Core.Logging;
using Steamworks;
namespace SQLPlayer
{
internal class DatabaseController
{
private MySqlConnection createConnection()
{
MySqlConnection Connection = null;
try
{
if (SQLPlayer.Instance.Configuration.Instance.DatabasePort == 0)
{
SQLPlayer.Instance.Configuration.Instance.DatabasePort = 3306;
}
Connection = new MySqlConnection(string.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};", new object[]
{
SQLPlayer.Instance.Configuration.Instance.DatabaseAddress,
SQLPlayer.Instance.Configuration.Instance.DatabaseName,
SQLPlayer.Instance.Configuration.Instance.DatabaseUsername,
SQLPlayer.Instance.Configuration.Instance.DatabasePassword,
SQLPlayer.Instance.Configuration.Instance.DatabasePort
}));
}
catch (Exception) { }
return Connection;
}
public DatabaseController()
{
new CP1250();
CheckSchema();
}
internal void CheckSchema()
{
try
{
MySqlConnection mySqlConnection = createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = "show tables like '" + SQLPlayer.Instance.Configuration.Instance.LogTableName + "'";
mySqlConnection.Open();
if (mySqlCommand.ExecuteScalar() == null)
{
mySqlCommand.CommandText = "CREATE TABLE `" + SQLPlayer.Instance.Configuration.Instance.LogTableName + "` ( `id` INT NOT NULL AUTO_INCREMENT , `player_display_name` TEXT NOT NULL , `player_steam_name` TEXT NOT NULL ,`player_steamgroup_name` TEXT NOT NULL , `player_steam_id` INT NOL NULL , `sent_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`));";
mySqlCommand.ExecuteNonQuery();
}
mySqlConnection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
// Token: 0x06000006 RID: 6 RVA: 0x00002250 File Offset: 0x00000450
public void addPlayer(string charactername, string steamname , string steamgroupname , int steamid)
{
try
{
MySqlConnection mySqlConnection = createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.Parameters.AddWithValue("@steamname", steamname);
mySqlCommand.Parameters.AddWithValue("@charactername", charactername);
mySqlCommand.Parameters.AddWithValue("@steamgroupname", steamgroupname);
mySqlCommand.Parameters.AddWithValue("@steamid", steamid);
string commandText = "insert into `" + SQLPlayer.Instance.Configuration.Instance.LogTableName + "` (`player_display_name` , `player_steam_name`,`player_steamgroup_name`,`player_steam_id`) values(@charactername,@steamname , @steamgroupname ,@steamid);";
mySqlCommand.CommandText = commandText;
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
}
catch (Exception) { }
}
public void DeletePlayers()
{
try
{
MySqlConnection mySqlConnection = createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
DateTime dateTime = DateTime.Now.AddDays(-SQLPlayer.Instance.Configuration.Instance.DeleteAfterDays);
mySqlCommand.Parameters.AddWithValue("@days", dateTime);
string commandText = "DELETE FROM " + SQLPlayer.Instance.Configuration.Instance.LogTableName + " WHERE `last_login` > @date; ";
mySqlCommand.CommandText = commandText;
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
}
catch (Exception) { }
}
}
}
时遇到的错误:
addPlayer
答案 0 :(得分:2)
你的问题就在这一行:
id.m_FriendsGroupID = (short)player.SteamGroupID.m_SteamID;
m_SteamID
值很可能是int
,而且太大而无法容纳short
类型的变量。这里最好的解决方案是确保id.m_FriendsGroupID
的类型为int
,并将广告素材移至short
。