【发布时间】:2012-06-19 02:34:09
【问题描述】:
我的 sql server 数据库中有一个字段类型 money,它是 moneyAcumulated。然后使用command 对象,我执行一个存储过程——除其他外——带来moneyAcumulated 列的值。然后在 while 循环中,我将存储过程返回的每一列的所有值存储在变量中。
SqlCommand command = new SqlCommand("getTickets", Connection.getConnection());
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
string bet = "";
decimal moneyAcumulated = 0.0M;
string id= "";
string name = "";
int cod = -1;
decimal prize = 0.0M;
while (reader.Read())
{
bet = reader.GetString(0);
moneyAcumulated = reader.GetDecimal(1); // This variable doesn't chage its value
name = reader.GetString(2);
id = reader.GetString(3);
cod = reader.GetInt32(4);
}
第一次while循环迭代每个变量取正确的值(包括moneyAcumulated带来reader,但第二次moneyAcumulated没有改变它的值而其他人这样做,为什么?
【问题讨论】:
-
您在 Management Studio 中查看过结果集吗?所有行的值可能都相同。此外,您将所有行中的值存储在相同的变量中。您可能希望使用列表或类似的东西来保存每一行。
-
是的,当我在调试时,我在每次迭代中都做了一个查询 (
select * from Money),以查看列moneyAcumulated的值,它在数据库中发生了变化,但在程序中没有变化。
标签: c# sqldatareader sqldatatypes type-equivalence