Skip to content
Loading
u can use like this
+1
  • +1
    The SQL COUNT() is a function that returns the number of records of the table in the output.
     
    SQL QUERY:
     
    SELECT COUNT(*) FROM emp
     
    Here I attached my screenshot:
     
     
    +1
  • Rijwan Ansari
    +1
    You can use below code.
     
    1. SqlConnection conn = new SqlConnection("ConnectionString");  
    2. conn.Open();  
    3. SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM InvData", conn);  
    4. Int32 count = (Int32) comm .ExecuteScalar();  
     
    +1
  • 0

    Try the snippet below, you may need to research some of the other classes to get your connectionString (DSN= etc), but this worked for me.

    Code
    using System.Data.Odbc
    C#
    public static void ReadData(string connectionString)
    {
        string queryString = "SELECT count(*) from INV";
    
        using (OdbcConnection connection = new OdbcConnection(connectionString))
        {
            OdbcCommand command = new OdbcCommand(queryString, connection);
    
            connection.Open();
    
            // Execute the DataReader and access the data.
            OdbcDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine("CustomerID={0", reader[0]);
            
    
            // Call Close when done reading.
            reader.Close();
    0
  • 0
    Try this. private void button2_Click(object sender, EventArgs e) { SqlConnection myConnectionAdd = null; DataSet myDataSetAdd = new DataSet(); myConnectionAdd = new SqlConnection("Data Source=YourSource Catalog=YourCatalog;Integrated Security=True"); string mySqlInsert = "SELECT count(*) FROM dbo.Customers"; SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); mySqlDataAdapter.SelectCommand = new SqlCommand(mySqlInsert, myConnectionAdd); mySqlDataAdapter.Fill(myDataSetAdd, "count(*)"); dataGridView1.DataSource = myDataSetAdd.Tables["count(*)"]; myConnectionAdd.Close(); }
    0
  • 0
    this is more of an edit to Jeff's post.  Not really necessary to use a DataReader object if just returning one column.

    C#
    public static void ReadData(string connectionString){    string queryString = "SELECT count(*) from INV";    using (OdbcConnection connection = new OdbcConnection(connectionString))    {        OdbcCommand command = new OdbcCommand(queryString, connection);        connection.Open();        int count = command.ExecuteScalar();        Console.WriteLine("Count is: {0}", count);    }}

    "ExecuteScalar()" returns the first column of the first record.  Since we are only return one column of one record, it is the perfect solution.

    Also, since you didn't state with database you were using, we chose to use the ODBC connection.  If you are using SQL Server, you can use "SqlConnection" and "SqlCommand" instead.
    0
  • 0
    Code
    cmd.CommandText = "SELECT COUNT(*) FROM InvData"; 
    Int32 count = (Int32) cmd.ExecuteScalar();
    
     above code helps you to make it working.
    0
  • 0

    public static int Count(string connectionString)
    {
    string query = "Select Count(*) From InvData";
    int count=""; 
    using (SqlConnection con = new SqlConnection(Your data source ConnectionString))
    {?SqlCommand cmd = new SqlCommand(query, con);?cmd.CommandType = CommandType.Text;?con.Open();?count = cmd.ExecuteScalar();?}
    return count;?}

    Here, Using statement is used so no need to close the connection, it will close automatically.
    ExecuteScalar is used to retrieve single value from database, so for counting the data ExecuteScalar can be used. 

    0
  • Jignesh Kumar
    0
    Hi Maria,
     
    Please refer this link which will help you out,
    https://www.aspsnippets.com/questions/616597/Count-number-of-rows-in-a-table-using-C-and-VBNet-in-ASPNet/ 
    0