即使GetDouble返回双重

发布于 2025-02-08 14:52:48 字数 1294 浏览 3 评论 0原文

在带有.NET框架的C#程序中,我添加了以下代码以建立连接并获得结果的值并存储在列表中以供以后使用,

public List<Info> GetInfo(string Col)
        {
            string connectionString = "Server=VIS-12\\TESTSQLSERVER;Database=SPCC;User Id=sa;Password=admin1234;";
            string comand = $"select {Col} from Tbl_Reading where [LogTime] between '2017-07-06 14:30:26.000' and '2017-07-06 14:30:26.000' ";
            using (SqlConnection conn = new SqlConnection())
            {
                double[] val1 = new double [100] ;
                conn.ConnectionString = connectionString;
                conn.Open();
                SqlCommand c1 = new SqlCommand(comand, conn);
                SqlDataReader reader = c1.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {

                    int x = 0;
                    val1[x] = Convert.ToDouble(reader.GetDouble(0)); //this line is throwing the error 
                    x++;
                }
            }
            reader.Close();
            List<Info> d1 = new List<Info>();
            d1 = (List<Info>)val1.Cast<Info>();//ToList();
            conn.Close();
            return d1;
            //return val1;
        }
    }

即使返回类型和变量类型相同

In a C# program with .NET framework to make a windows form I have added the following code to make a connection and to get the value of result and store in a list for later usage

public List<Info> GetInfo(string Col)
        {
            string connectionString = "Server=VIS-12\\TESTSQLSERVER;Database=SPCC;User Id=sa;Password=admin1234;";
            string comand = 
quot;select {Col} from Tbl_Reading where [LogTime] between '2017-07-06 14:30:26.000' and '2017-07-06 14:30:26.000' ";
            using (SqlConnection conn = new SqlConnection())
            {
                double[] val1 = new double [100] ;
                conn.ConnectionString = connectionString;
                conn.Open();
                SqlCommand c1 = new SqlCommand(comand, conn);
                SqlDataReader reader = c1.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {

                    int x = 0;
                    val1[x] = Convert.ToDouble(reader.GetDouble(0)); //this line is throwing the error 
                    x++;
                }
            }
            reader.Close();
            List<Info> d1 = new List<Info>();
            d1 = (List<Info>)val1.Cast<Info>();//ToList();
            conn.Close();
            return d1;
            //return val1;
        }
    }

I still keep getting an invalid cast Exception even though the return type and the variable type is same

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2025-02-15 14:52:48

getDouble确实返回类型double,但是必须有一个double值才能获得。在内部,该方法将从指定的列中获取对象参考,并将其施放为Type double。正是这种演员无效,而不是您正在执行的演员。如果您查看异常的堆栈跟踪,则应该可以看到。

要么要检索的列是错误的数据类型,要么至少一行包含该列中的null。如果数据类型是正确的,则需要在查询中过滤零值,或者在阅读代码中对其进行计算。

GetDouble does indeed return type double but there has to be a double value to get. Internally, that method will get an object reference from the specified column and cast it as type double. It is that cast that is invalid, not one that you're performing. If you look at the stack trace of the exception then you should be able to see that.

Either the column you're retrieving is the wrong data type or at least one row contains NULL in that column. If the data type is correct then you need to either filter out NULL values in your query or else account for them in your reading code.

忆悲凉 2025-02-15 14:52:48

getDouble如果内部的值不是double,则会引发异常。如果列是不同的类型,但如果值为null,则显然会发生这种情况。因此,您需要处理这种情况。

其他注意事项:

  • if(reader.hasrows)是不需要的,因为您可以使用reader.read()告诉您同一件事。
  • 使用带有@的多行字符串使查询更可读。
  • 我希望col不会来自用户输入,否则您会有重大的注射漏洞。
  • 您缺少使用块来处理对象的。
  • 似乎不需要数组,只需转换每个值并插入列表中即可。
  • 之间的<代码>在这里似乎没有意义,您也可能应该在日期使用参数。如果实际上有一个开始和结束时间,请使用&gt; = and&lt;,而不是之间的
  • 考虑将连接字符串放在设置文件中,而不是对其进行硬编码。
public List<Info> GetInfo(string Col)
{
    string connectionString = "Server=VIS-12\\TESTSQLSERVER;Database=SPCC;User Id=sa;Password=admin1234;";
    string comand = @$"
select {Col}
from Tbl_Reading
where LogTime = @time;
";

using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand c1 = new SqlCommand(comand, conn))
{
    c1.Parameters.Add("@time", SqlDbType.DateTime).Value = DateTime.Parse("2017-07-06 14:30:26.000");
    List<Info> d1 = new List<Info>();
    conn.Open();
    using (SqlDataReader reader = c1.ExecuteReader())
    {
        while (reader.Read())
        {
            d1.Add((reader[0] as double) as Info);
        }
        return d1;
    }
}

GetDouble will throw an exception if the value internally is not a double. This will happen obviously if the column is a different type, but also if the value is a null. So you need to handle that case.

Other notes:

  • if (reader.HasRows) is not necessary as you can use reader.Read() to tell you the same thing.
  • Use a multi-line string with @ to make your query more readable.
  • I hope Col is not coming from user-input, otherwise you have a major injection vulnerability.
  • You are missing using blocks to dispose your objects.
  • There seems to be no need for the array, just convert each value as you go along and insert into a list.
  • between doesn't seem to make sense here, also you should probably use a parameter for the date. If there is really a start and end time, use >= AND < rather than BETWEEN.
  • Consider putting the connection string in a settings file, rather than hard-coding it.
public List<Info> GetInfo(string Col)
{
    string connectionString = "Server=VIS-12\\TESTSQLSERVER;Database=SPCC;User Id=sa;Password=admin1234;";
    string comand = @
quot;
select {Col}
from Tbl_Reading
where LogTime = @time;
";

using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand c1 = new SqlCommand(comand, conn))
{
    c1.Parameters.Add("@time", SqlDbType.DateTime).Value = DateTime.Parse("2017-07-06 14:30:26.000");
    List<Info> d1 = new List<Info>();
    conn.Open();
    using (SqlDataReader reader = c1.ExecuteReader())
    {
        while (reader.Read())
        {
            d1.Add((reader[0] as double) as Info);
        }
        return d1;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文