tags:

views:

179

answers:

3

I am trying to return the result that I found in my query to the ASP.net table. How do I do that? I already have the query, I am just having trouble getting the count result back.

string configMan.ConnString["connect"].ToString();
iDB2Conn temp = new iDB2Conn
string query = "select Count(*) as total from test";
...

this is where I am having trouble.

A: 

In ADO.Net, the simplest way is to use the ExecuteScalar() method on your command which returns a single result. You don't explicitly list what database or connection method you are using, but I would expect that most database access methods have something equivalent to ExecuteScalar().

Joe Doyle
A: 

Try using the ExecuteScalar method on your command. You should be able to use the generic one or cast the result to an int/long.

JD Conley
+9  A: 

This is where the SqlCommand object comes in handy.

int result = 0;
using(SqlConnection conn = new SqlConnection(connectionString))
{
    conn.Open();
    SqlCommand sql = new SqlCommand("SELECT COUNT(*) FROM test", conn);
    result = (int)sql.ExecuteScalar();
}
tghw
worked wonderful... THANKS!
Glad I could help!
tghw