c#访问数据库sql 读取单元格中存在小数 string sqls = $select MeasurementPointsName from MeasurementPointDataRecordTableproduction whereProductionProductNum= product ; 读取相应的单元格数据该单元格存在小数
您可以使用 SqlDataReader 对象来读取单元格中的小数值,示例代码如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sqls, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) // 读取第一行数据
{
decimal value = reader.GetDecimal(0); // 获取第一列的小数值
Console.WriteLine($"单元格中的小数值为:{value}");
}
reader.Close();
}
这里使用 SqlDataReader 的 GetDecimal 方法来获取单元格中的小数值,方法的参数为列索引,从 0 开始。因为您只查询了一个单元格,所以列索引为 0。
请注意,上述代码中的 connectionString 变量应该是您的数据库连接字符串,可以在代码中进行定义或者从配置文件中读取。另外,需要将 Console.WriteLine 替换为您需要执行的操作,例如将小数值写入到某个变量或者进行其他计算。
原文地址: https://www.cveoy.top/t/topic/bumw 著作权归作者所有。请勿转载和采集!