在C#中读取SQL Server数据库中的"真实"数据类型

Jam*_*are 1 .net c# sql-server-ce

我有一个从SQL Server Compact数据库中提取数据的方法:

// Open the same connection with the same connection string.
using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
{
   con.Open();
   // Read specific values in the table.
   using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM CpuResults WHERE Date = @date", con))
   {
      List<float> results = new List<float>();
      com.Parameters.AddWithValue("date", Form1.date);
      SqlCeDataReader reader = com.ExecuteReader();

      while (reader.Read())
      {
         float resultsoutput = reader.GetInt32(0);
         results.Add(resultsoutput);
      }
Run Code Online (Sandbox Code Playgroud)

cpuResults上"Result"列中结果的"type"定义为Real.我试图将这些数据转换为浮点格式,因为Result列中的数据是例如0.02和1.23等.虽然当我运行我的方法时,我得到:

指定演员表无效.

如果我将列的数据类型更改Result为int,则不会发生此问题.

Ode*_*ded 5

这一行:

float resultsoutput = reader.GetInt32(0);
Run Code Online (Sandbox Code Playgroud)

您正在尝试获取整数并将值放入a float.

要么得到一个float开始:

float resultsoutput = reader.GetFloat(0);
Run Code Online (Sandbox Code Playgroud)

或者更改变量的类型:

int resultsoutput = reader.GetInt32(0);
Run Code Online (Sandbox Code Playgroud)