Reading values from SQL database in C#

前端 未结 5 1020
陌清茗
陌清茗 2020-12-14 01:45

i have just started learning C# and i can write data to the database without a problem. But i\'m having problems with reading, the SQL executes fine but i\'m having issues w

5条回答
  •  误落风尘
    2020-12-14 02:32

    One problem is missing braces after the while

    while (myReader.Read())
    {  // <<- here
        Console.WriteLine(myReader["Username"].ToString());
        Console.WriteLine(myReader["Item"].ToString());
        Console.WriteLine(myReader["Amount"].ToString());
        Console.WriteLine(myReader["Complete"].ToString());
    }  // <<- here
    

    if you skip the braces only the first line will be processed in each loop, the rest will be processed after the loop, then myReader is past the last row.

提交回复
热议问题