简体   繁体   English

C#System.InvalidCastException:'指定的转换无效。' 将MySQL与Discord.Net一起使用时

[英]C# System.InvalidCastException: 'Specified cast is not valid.' When using MySQL with Discord.Net

I've been getting errors which are terrible :D 我一直在得到可怕的错误:D

        public static List<tableName> GetUserStatus(IUser user)
    {
        var result = new List<tableName>();

        var database = new Database("dibot");

        var str = string.Format("SELECT * FROM dibott WHERE userid = '{0}'", user.Id);
        var tableName = database.FireCommand(str);

        while (tableName.Read())
        {
            var userId = (string)tableName["userid"];
            var userName = (string)tableName["username"];
            var currentTokens = (int)tableName["tokens"]; // Here

            result.Add(new tableName
            {
                userid = userId,
                username = userName,
                tokens = currentTokens
            });
        }
        database.CloseConnection();

        return result;

    }

At this command 在此命令

        [Command("Status")]
    [Alias("status", "s")]
    public async Task Status([Remainder] IUser user = null)
    {
        var embed = new EmbedBuilder()
        {
            Color = new Color(0, 0, 255)
        };
        if (user == null)
        {
            user = Context.User;
        }

        var result = Database.CheckExistingUser(user); //Here

        if (result.Count() <= 0)
        {
            Database.EnterUser(user);
        }

        var tableName = Database.GetUserStatus(user);

I've tried everything please help. 我已经尝试了一切,请帮忙。 I tried much things. 我尝试了很多事情。 Nothing helped Asking for help because im out of ideas and in internet there arent any fixes I've put // To see where are the errors. 什么都没有帮助寻求帮助,因为即时通讯出于想法和互联网的存在,我没有进行任何修复//以查看错误在哪里。 I hope i get helped soon! 希望我能尽快得到帮助! :) :)

So the problematic line is this: 因此,有问题的行是这样的:

var currentTokens = (int)tableName["tokens"];

InvalidCastException means that the type contained in tableName["tokens"] is not int . InvalidCastException表示tableName["tokens"]包含的类型不是 int To get at the actual type you can for example remove the cast, put a breakpoint on that line and check with the debugger. 要获得实际的类型,您可以例如删除强制转换,在该行上放置一个断点,然后与调试器进行检查。

From your comment we know that the database type is a varchar, which means you need to use string in .NET: 根据您的评论,我们知道数据库类型是varchar,这意味着您需要在.NET中使用字符串:

var currentTokens = (string)tableName["tokens"];

since you are casting tokens into int im assuming its a numeric column. 因为您将令牌转换为int im并假定其为数字列。

If you have TINYINT then use byte , if its unsigned then use ubyte 如果您有TINYINT则使用byte ;如果其未签名,则使用ubyte
if you have SMALLINT then use short , or ushort if its unsigned 如果您有SMALLINT则使用short ,如果未签名,则使用ushort
if you have MEDIUMINT/INT then use int or uint for unsigned 如果您有MEDIUMINT/INT则将intuint用作未签名
if you have BIGINT then use long or ulong for unsigned 如果您有BIGINT则将longulong用于未签名

best way to find out is to do a tableName["tokens"].GetType() . 找出tableName["tokens"].GetType()最佳方法是执行tableName["tokens"].GetType()

If you don't cast it to the right type you will get that InvalidCast exception 如果您不将其转换为正确的类型,则将收到该InvalidCast异常

Edit: 编辑:

Based on your comment its a varchar column which means its a string. 根据您的评论,它的varchar列表示它是一个字符串。 if you want to make it an int then you need to do 如果要使其为整数,则需要

int.Parse((string) tableName["tokens"]);  

or 要么

long.Parse((string) tableName["tokens"]);  

if its a 64 bit int 如果它是64位int

But it is really a bad design to store numeric in string format and later on parse it again. 但是,将数字存储为字符串格式并随后再次解析它确实是一个糟糕的设计。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL System.InvalidCastException:&#39;指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query System.InvalidCastException:指定的强制转换无效。错误 - System.InvalidCastException: Specified cast is not valid. Error Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' 在 xamarin 项目 System.InvalidCastException:“指定的演员表无效。” - in xamarin project System.InvalidCastException: 'Specified cast is not valid.' C#System.InvalidCastException:指定的转换无效 - C# System.InvalidCastException: Specified Cast is not valid System.InvalidCastException:指定的强制转换无效的C# - System.InvalidCastException: Specified cast is not valid C# System.InvalidCastException:使用postgres datareader在C#中指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid in C# using postgres datareader 使用LINQ to SQL和sqlmetal的C#中的错误System.InvalidCastException:指定的强制转换无效 - Error in C# using LINQ to SQL and sqlmetal System.InvalidCastException: Specified cast is not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM