从查询结果中读取单个值

时间:2022-10-25 08:24:21

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.

我试图将我在查询中找到的结果返回到ASP.net表。我怎么做?我已经有了查询,我只是无法获得计数结果。

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

this is where I am having trouble.

这是我遇到麻烦的地方。

3 个解决方案

#1


This is where the SqlCommand object comes in handy.

这是SqlCommand对象派上用场的地方。

int result = 0;
using(SqlConnection conn = new SqlConnection(connectionString))
{
    conn.Open();
    SqlCommand sql = new SqlCommand("SELECT COUNT(*) FROM test", conn);
    result = (int)sql.ExecuteScalar();
}

#2


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().

在ADO.Net中,最简单的方法是在命令上使用ExecuteScalar()方法,该方法返回单个结果。您没有明确列出您正在使用的数据库或连接方法,但我希望大多数数据库访问方法都具有与ExecuteScalar()相同的功能。

#3


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.

尝试在命令中使用ExecuteScalar方法。您应该能够使用泛型或将结果转换为int / long。

#1


This is where the SqlCommand object comes in handy.

这是SqlCommand对象派上用场的地方。

int result = 0;
using(SqlConnection conn = new SqlConnection(connectionString))
{
    conn.Open();
    SqlCommand sql = new SqlCommand("SELECT COUNT(*) FROM test", conn);
    result = (int)sql.ExecuteScalar();
}

#2


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().

在ADO.Net中,最简单的方法是在命令上使用ExecuteScalar()方法,该方法返回单个结果。您没有明确列出您正在使用的数据库或连接方法,但我希望大多数数据库访问方法都具有与ExecuteScalar()相同的功能。

#3


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.

尝试在命令中使用ExecuteScalar方法。您应该能够使用泛型或将结果转换为int / long。