2017-02-28 63 views
0

我想用SQL做一個插件,我得到一些錯誤,我不知道如何處理它們。 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我收到錯誤:

Error in MulticastDelegate PlayerConnected: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OverflowException: Value is too large 
    at System.Int16.Parse (System.String s) [0x00000] in <filename unknown>:0 
    at SQLPlayer.SQLPlayer.Events_OnPlayerConnected (Rocket.Unturned.Player.UnturnedPlayer player) [0x00000] in <filename unknown>:0 
    at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) 
    at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
    --- End of inner exception stack trace --- 
    at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
    at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <filename unknown>:0 
    at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in <filename unknown>:0 
    at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in <filename unknown>:0 
    at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in <filename unknown>:0 
    at Rocket.Core.Extensions.MulticastDelegateExtension.TryInvoke (System.MulticastDelegate theDelegate, System.Object[] args) [0x00000] in <filename unknown>:0 
+0

你的異常在'Events_OnPlayerConnected'中,而不在'addPlayer'中。堆棧跟蹤告訴你這一點。 – DavidG

+0

錯誤非常明顯,您正嘗試將一個數字填充到Int16中,該Int16太大而無法容納Int16。你可能需要一個Int32('int')甚至一個Int64('long') – Crowcoder

+0

@DavidG我需要做什麼? – Plugin4U

回答

2

您的問題是這一行:

id.m_FriendsGroupID = (short)player.SteamGroupID.m_SteamID; 

The m_SteamID值可能是int,並且太大而不適合short類型的變量。這裏最好的解決方案是確保id.m_FriendsGroupID也是int類型,並將演員移除到short

+0

非常感謝大衛! – Plugin4U

+0

private void Events_OnPlayerConnected(UnturnedPlayer player) { FriendsGroupID_t id; id.m_FriendsGroupID = Convert.ToInt16(player.SteamGroupID.m_SteamID); Convert.ToInt32(player.CSteamID.m_SteamID); } – Plugin4U

+0

http://i.imgur.com/Gr4UDOv.png – Plugin4U