ExecuteScalar不在读卡器中工作?

时间:2014-07-16 16:47:40

标签: c# asp.net sql

当我尝试从我的数据库中获取1列的值时(使用ExecuteScalar(),字符串或整数没有任何作用)它在执行时崩溃。

是因为我在我的公开ExecuteScalar()中使用SqlDataReader吗?

以下是我的代码,它在 iAantal 时崩溃:

SqlCommand get_order = new SqlCommand("SELECT * FROM Factuur WHERE ID = @ID1 OR order_id = @ID2", con);
get_order.Parameters.AddWithValue("@ID1", Session["LastOrderID"].ToString());
get_order.Parameters.AddWithValue("@ID2", Session["LastOrderID"].ToString());

SqlDataReader rdrOrder = get_order.ExecuteReader();
iAantal = 2;
while(rdrOrder.Read())
{
    from_db_producten += "<tr>";

    sHuidigeDatum = rdrOrder["besteldatum"].ToString();
    sLeverdatum = rdrOrder["leverdatum"].ToString();
    sToestelID = rdrOrder["item_id"].ToString();

    iPrijsPerStuk = Convert.ToInt32(rdrOrder["prijs"]);


    SqlCommand check_aantal_toestel = new SqlCommand("SELECT COUNT(item_id) FROM Factuur WHERE ID = @orderID1 OR order_id = @orderID2 AND item_id = @itemID", con);
    check_aantal_toestel.Parameters.AddWithValue("@orderID1", Session["LastOrderID"].ToString());
    check_aantal_toestel.Parameters.AddWithValue("@orderID2", Session["LastOrderID"].ToString());
    check_aantal_toestel.Parameters.AddWithValue("@itemID", sToestelID);


    iAantal = Convert.ToInt32(check_aantal_toestel.ExecuteScalar());

    SqlCommand get_toestel_merk = new SqlCommand("SELECT item_value FROM ItemSpecificatie WHERE item_key = @merk AND item_id = @ID", con);
    get_toestel_merk.Parameters.AddWithValue("@merk", "Merk");
    get_toestel_merk.Parameters.AddWithValue("@ID", sToestelID);

    SqlCommand get_toestel_naam = new SqlCommand("SELECT item_value FROM ItemSpecificatie WHERE item_key = @naam AND item_id = @ID", con);
    get_toestel_merk.Parameters.AddWithValue("@naam", "Naam");
    get_toestel_merk.Parameters.AddWithValue("@ID", sToestelID);

    sToestelmerk = Convert.ToString(get_toestel_merk.ExecuteScalar());
    sToestelnaam = Convert.ToString(get_toestel_naam.ExecuteScalar());

    iPrijsTotaal = iAantal * iPrijsPerStuk;
}

1 个答案:

答案 0 :(得分:0)

正如您的问题本身所述,当ExecuteScalar()打开时,您无法执行ExecuteReader()

但是,您可以在连接字符串提供程序部分中添加MultipleActiveResultSets = true以实现与数据源的多个连接。

但是,这是可行的,这不是数据库连接的推荐方法。因为,打开与数据源的多个连接将导致数据库的额外负载。