即使GetDouble返回双重
在带有.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getDouble
确实返回类型double
,但是必须有一个double
值才能获得。在内部,该方法将从指定的列中获取对象
参考,并将其施放为Typedouble
。正是这种演员无效,而不是您正在执行的演员。如果您查看异常的堆栈跟踪,则应该可以看到。要么要检索的列是错误的数据类型,要么至少一行包含该列中的null。如果数据类型是正确的,则需要在查询中过滤零值,或者在阅读代码中对其进行计算。
GetDouble
does indeed return typedouble
but there has to be adouble
value to get. Internally, that method will get anobject
reference from the specified column and cast it as typedouble
. 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.
getDouble
如果内部的值不是double
,则会引发异常。如果列是不同的类型,但如果值为null,则显然会发生这种情况。因此,您需要处理这种情况。其他注意事项:
if(reader.hasrows)
是不需要的,因为您可以使用reader.read()
告诉您同一件事。@
的多行字符串使查询更可读。col
不会来自用户输入,否则您会有重大的注射漏洞。&gt; = and&lt;
,而不是之间的。
GetDouble
will throw an exception if the value internally is not adouble
. 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 usereader.Read()
to tell you the same thing.@
to make your query more readable.Col
is not coming from user-input, otherwise you have a major injection vulnerability.using
blocks to dispose your objects.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 thanBETWEEN
.