我正在尝试使用C#的SqlDataReader
查找表的计数,但我一直收到
无效的读取尝试,因为没有数据存在
我的代码:
string sql = "SELECT COUNT(*) FROM [DB].[dbo].[myTable]";
SqlCommand cmd = new SqlComman(sql, connectionString);
SqlDataReader mySqlDataReader = cmd.ExecuteReader();
int count = mySqlDataReader.GetInt32(0); // Here is where I get the error.
我知道我与数据库有有效的连接,因为我可以在许多地方读写它,那么COUNT(*)
有什么特别之处,不能正确读取它?如何使int count
被填充?