none
想请帮忙看看有关reader的错误 RRS feed

  • 问题

  • 以下是我的代码?

      using (var con = new OleDbConnection())
                    {
                        con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;";
                        con.Open();
                        var cmd = new OleDbCommand();
                        cmd.Connection = con;
                        foreach (var key in dic.Keys)
                        {
                            cmd.CommandText = "SELECT Qty,TotalPrice FROM tbl_Product Where ID = " + key;
                            var reader = cmd.ExecuteReader; (我在这段 error Cannot assign method group to an implicitly-typed local variable)
                            while (reader.Read)
                            {
                                total_Cart_Qty = Convert.ToInt32(reader("Qty"));
                                total_price = Convert.ToInt32(reader("TotalPrice"));
                            }

                            reader.Close();

    请指教

    2021年5月24日 3:16

全部回复

  • 你好,

    ExecuteReader是一个返回OleDbDataReader类型的方法,在c#中使用方法是需要加括号的。(下面的read.Read也要加括号)

    OleDbDataReader reader = cmd.ExecuteReader();             
                        while (reader.Read())
                        {
                            total_Cart_Qty = Convert.ToInt32(reader("Qty"));
                            total_price = Convert.ToInt32(reader("TotalPrice"));
                        }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2021年5月24日 9:23
    版主