473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

count records in table

Hi!

I am trying to find the number of records stored in my table within a
MS SQL database.
This is part of a Console Application.
Here is my code:

Sub RecordCount()
Dim cmd2 As New System.Data.SqlClient.SqlCommand("SELECT
COUNT(*) FROM Keyword", conn)
Dim count As Integer
count = cmd2.ExecuteNonQuery()
Console.WriteLine(count)
End Sub

When I run this, the console displays "-1".

How do I write the code in such a way that I can execute the SQL
command
SELECT COUNT(*) FROM Keyword
and store the result in a variable?

Thanks,

Etay

Apr 19 '07 #1
1 3487
Etay,

ExecuteScalar, not ExecuteNonQuery.

Kerry Moorman
"Etayki" wrote:
Hi!

I am trying to find the number of records stored in my table within a
MS SQL database.
This is part of a Console Application.
Here is my code:

Sub RecordCount()
Dim cmd2 As New System.Data.SqlClient.SqlCommand("SELECT
COUNT(*) FROM Keyword", conn)
Dim count As Integer
count = cmd2.ExecuteNonQuery()
Console.WriteLine(count)
End Sub

When I run this, the console displays "-1".

How do I write the code in such a way that I can execute the SQL
command
SELECT COUNT(*) FROM Keyword
and store the result in a variable?

Thanks,

Etay

Apr 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr...
6
by: John | last post by:
I've got a single table I need to query to return records that have no "related records." Table dataset example: 1. John, Biology 2. Dave, Math 3. Susan, Biology 4. Betty, Sociology The...
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
2
by: Lauren Quantrell | last post by:
I am using a stored procedure as the recordsource on an MS-Access2000 form: Forms!frmName.RecordSource = "dbo.myStoredProcedure" The stored procedure creates a temp table #Contacts and then...
4
by: Peter W Johnson | last post by:
Hi guys, I have a problem with a datagrid record count. Here is the code:- <snip> Public Class frmMerchantDeposit Inherits System.Windows.Forms.Form Dim myconnection As New...
4
by: Anuradha | last post by:
Dear Experts How can I count records in a data reader? Thx Anuradha
2
by: rn5a | last post by:
A SQL Server 2005 stored procedure expects a parameter 'UserID' depending upon which it retrieves the no. of records & OrderIDs corresponding to the 'UserID' from a DB table (note that OrderID &...
9
by: MLH | last post by:
A mailing list table in its virgin state contained sequential, consecutive integers in an autonumber field (A97). I've deleted records throughout the table. Now I would like to identify each...
6
markrawlingson
by: markrawlingson | last post by:
Hopefully someone can help me out with this, it's driving me nuts... I have two tables - We'll call them table1 and table2. Table1 holds event information, and table2 holds user registration data...
12
by: petter | last post by:
Hi! I have two questions: one question that regards the COUNT-function, and one about how to display a month even if I don’t have any data for that month. I have an Access database where I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.