c# - C# 控制台中的 System.InvalidCastException

标签 c#

当我执行以下代码时,出现错误提示。

System.InvalidCastException: Specified cast is not valid.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
   at ProbabilityFunctions.Program.Main(String[] args) in C:\Users\....:line 38Press any key to continue . . .

我在这段代码中试图做的是,我从一个表中检索数据并将其存储到另一个表中。我将此表作为参数传递给分类器以进行进一步操作。请告诉我错误。 height,uname的数据类型是varchar...其他都是int...

using System;
    using System.Data;
    using MySql.Data.MySqlClient;

    namespace ProbabilityFunctions
    {
        public class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    String con = "SERVER=localhost;DATABASE=sample;UID=root;password=password";
                    MySqlConnection conn = new MySqlConnection(con);
                    String s = "select * from cart";
                    MySqlCommand cmd = conn.CreateCommand();
                    MySqlCommand cmd2 = conn.CreateCommand();
                    cmd2.CommandText = "select count(*) from cart";
                    conn.Open();
                    int z = Convert.ToInt32(cmd2.ExecuteScalar());

                    conn.Close();
                    MySqlDataAdapter dat = new MySqlDataAdapter(s, conn);
                    DataTable tbl = new DataTable();

                    dat.Fill(tbl);
                    cmd.CommandText = s;
                    DataTable table = new DataTable();
                    table.Columns.Add("name");
                    table.Columns.Add("Height", typeof(double));
                    table.Columns.Add("cost", typeof(double));
                    table.Columns.Add("FootSize", typeof(double));


                    for (int i = 0; i < z; i++)
                    {
                        DataRow row = tbl.Rows[i];
                        Double height = row.Field<Double>("height");
                        Double fsize = row.Field<Double>("fsize");
                        Double cost = row.Field<Double>("cost");
                        String uname = row.Field<String>("uname");
                        table.Rows.Add(uname, height, cost, fsize);
                    }


                    Classifier classifier = new Classifier();
                    classifier.TrainClassifier(table);

                    Console.WriteLine(classifier.Classify(new double[] { 4, 150, 12 }));
                    Console.Read();
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
                }
        }
    }

最佳答案

这可能发生在以下线路之一:

Double height = row.Field<Double>("height");
Double fsize = row.Field<Double>("fsize");
Double cost = row.Field<Double>("cost");

如果基础字段类型不能直接转换为 double 类型,您将收到此异常。来自documentation for Field<T> , InvalidCastException 在以下情况下抛出:

The value type of the underlying column could not be cast to the type specified by the generic parameter, T.

鉴于堆栈跟踪,基础表中的“高度”列似乎实际上不是数字,并且不能转换为 double 。例如,如果它在输入数据集中存储为字符串,则您必须先提取一个字符串,然后再将其转换为 double 。

关于c# - C# 控制台中的 System.InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19912738/

相关文章:

c# - 剑道按钮 `k-danger` 不工作

c# - 从 where 子句或 IQueryable/IEnumerable 获取 lambda 表达式

c# - 为 Windows Phone 库解决方案运行单元测试

c# - LINQ 语句中的条件 IF 语句

c# - 如何从 MySQL 获取所有可用数据库到 C# Windows 窗体中的组合框控件

C# 事件作为方法中的参数

c# - x :FieldModifier ="Private" do and should I worry about it? 是什么

C#:安卓手机控件

c# - 通过创建更多文件写入文件(而不是替换/覆盖)

c# - 非英文操作系统Everyone组名