Display number of results of a SQL query [closed]

0

I have a resident registration system, I would like to display the quantity of records that result from my SQL lookup on my form in Visual Studio

The SQL code is as follows

SELECT * FROM PERGUNTAS WHERE BAIRRO = 'MCMV - INOÃ'

In SQL Server Management Studio gives me a result of 601 lines, I would like to display this 601 in the Visual Studio form.

    
asked by anonymous 11.03.2018 / 21:02

2 answers

1

The following code returns the count in visual studio, then you can put in the label or textbox that you want Pass to a SqlDataAdapter the query and then go fetch the .Rows.Count.

using System;

using System.Data;

using System.Data.SqlClient;

private DataTable dataTable = new DataTable();

public void PullData()
{
    string connString = @"your connection string here";
    string query = "SELECT * FROM PERGUNTAS WHERE BAIRRO = 'MCMV - INOÃ'";

    SqlConnection conn = new SqlConnection(connString);        
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();

    // criar o data adapter
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    // carrega os dados do sql para a data adapter
    da.Fill(dataTable);

    // retorna o nº de linhas.
    label.text = da.Rows.Count

    conn.Close();
    da.Dispose();
}
    
12.03.2018 / 16:26
0
SELECT COUNT(*), perguntas.* FROM perguntas WHERE bairro = 'MCMV - INOÃ'

COUNT(*) returns the number of rows

    
11.03.2018 / 21:04