如何检查sql查询是否返回值?

时间:2014-05-21 18:38:40

标签: c# sql sql-server

我有一个SQL查询。只是我想检查查询返回值或不使用c#。任何人都可以帮助解决这个问题。提前谢谢。

Sqlcommand cmd = new Sqlcomman("select Name from Engdetails",con)

if (query != 0)
{
  some coding..
}
else
{
  coding...
}

4 个答案:

答案 0 :(得分:4)

Sqlcommand cmd = new Sqlcomman("select Name from Engdetails",con)

SqlDataReader reader = cmd.ExecuteReader();

if (reader.Read())
{
   // The command returns Row(s)
}
else
{
   // No Row has been returned.
}

答案 1 :(得分:1)

有很多方法可以使用sqlCommand;一些例子:

using (SqlDataReader reader = cmd.ExecuteReader())
{
  if (reader.HasRows)
  {
    reader.Read();
...

using (SqlCommand cmd = new SqlCommand(query, connection))
{
   using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
   {
      adapter.Fill(data);
   }

如果有数据,则数据将在data.Tables

答案 2 :(得分:1)

SqlCommand cmd = new SqlCommand("select Name from Engdetails", con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
    // somecoding
}
else
{
    // somecoding
}

答案 3 :(得分:0)

在这种情况下的示例rdr.GetString是查询的Name值。

using (MySqlConnection conn = new MySqlConnection(constr))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        MySqlDataReader rdr = null;


        string[] dados_bd = new string[2];

        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = sql;

        rdr = cmd.ExecuteReader();
        while (rdr.Read())
            {
                if (estado == 1)
                {

                    dados_bd[0] = rdr.GetString(0);