淘先锋技术网

首页 1 2 3 4 5 6 7

I try to get number of rows from a table with this :

string commandLine = "SELECT COUNT(*) FROM client";

using (MySqlConnection connect = new MySqlConnection(connectionStringMySql))

using (MySqlCommand cmd = new MySqlCommand(commandLine, connect))

{

connect.Open();

int count = (int)cmd.ExecuteScalar();

return count;

}

And i get the exception:

Specified cast is not valid.

Any idea how i can fix it?

解决方案

Try this

using (MySqlCommand cmd = new MySqlCommand(commandLine, connect))

{

connect.Open();

return Convert.ToInt32(cmd.ExecuteScalar());

}