简体   繁体   English

C# SqlDataAdapter 将 Decimal 列中的所有值归零

[英]C# SqlDataAdapter turns all values in Column of Decimal to zero

I am using SqlDataAdapter to fill a DataSet from a SQLServer table.我正在使用 SqlDataAdapter 从 SQLServer 表中填充数据集。 All of the columns are filled properly except the Transaction column which is a Decimal type in the database.除了数据库中的十进制类型的事务列之外,所有列都已正确填充。 These values all become 0.0.这些值都变为 0.0。

The code:编码:

using (SqlConnection dbconn = new SqlConnection (connection_string)) {
            SqlDataAdapter adapter = new SqlDataAdapter (query, dbconn);
            exposureSequence = new DataSet ();
            adapter.Fill (exposureSequence, "ExposureSequence");
        }

This code这段代码

            foreach(DataRow r in exposureSequence.Tables["ExposureSequence"].Rows){
            Console.WriteLine(r["TransactionAmount"] + ", ");
        }

prints印刷

0.0000, 
0.0000, 
0.0000,
...

But if I do this:但如果我这样做:

using (IDataReader reader = dbcmd.ExecuteReader()) {
                    while(reader.Read()){
                        //int ActionClientID = (int) reader["ActionClientID"];
                        Decimal TransactionAmount = (Decimal) reader["TransactionAmount"];
                        Console.WriteLine(TransactionAmount);
                    }

My output is:我的 output 是:

164.4950
164.4950
164.4950
...

Why is adapter.Fill turning the TransactionAmount values to 0.000?为什么 adapter.Fill 将 TransactionAmount 值变为 0.000?

I don't see a difference between the 2 versions of code you posted.我看不出您发布的两个版本的代码之间有什么区别。 You should receive the same results.您应该会收到相同的结果。 If you execute the same query by using a dataReader or a dataAdapter the difference is how they execute and not the results you get.如果您使用 dataReader 或 dataAdapter 执行相同的查询,区别在于它们的执行方式而不是您获得的结果。 Do you use the same query for execution?您是否使用相同的查询来执行? If you do, I believe the problem maybe somewhere else (and not in the difference of the 2 versions).如果你这样做,我相信问题可能在其他地方(而不是两个版本的差异)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM