How do I count the number of rows returned in my SQLite reader in C#?

前端 未结 11 2207
生来不讨喜
生来不讨喜 2021-01-11 13:10

I\'m working in Microsoft Visual C# 2008 Express and with SQLite.

I\'m querying my database with something like this:

SQLiteCommand cmd = new SQLiteC         


        
11条回答
  •  北恋
    北恋 (楼主)
    2021-01-11 14:10

    Surely a better way to get a row count would be something like this:-

     SQLiteDataReader reader = SendReturnSQLCommand(dbConnection, "SELECT COUNT(*) AS rec_count FROM table WHERE field = 'some_value';");
          if (reader.HasRows) {
             reader.Read();
            int  count = Convert.ToInt32(reader["rec_count"].ToString());
    
    ...
    
        }
    

    That way you don't have to iterate over the rows

提交回复
热议问题