Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to count the no.of records of table in SQL server and save it in a variable from C#.net
Posted

use this code.
C#
string connectionString = "Data Source=youServerHere;Initial Catalog=YourDataBase;User ID=sa; Password=123;";

System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);

sqlConnection.Open();
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM tblProduct");
            sqlCommand.Connection = sqlConnection;
 
int RecordCount = Convert.ToInt32(sqlCommand.ExecuteScalar());
 
Share this answer
 
Comments
2012programmer 15-Jan-13 7:17am    
Thank you very much Every body.This code is working.I need some more help. I was saving the selected list items of a dropdown list into column of a database table.My requirement is to count the records with each "list item". If there are 10 records with one "listitem" then that listitem should be removed from the dropdown list.
The above code is working fine for one list item, But I need for 7 list items. Do I need to write the stored procedure or I need to write select statements 7 times.
Faisalabadians 15-Jan-13 7:23am    
as you are saying you have stored it in database and if there are 10 records for any particular list item, then you must use stored procedure to do that and return only those record, which don't have a count of 10.
2012programmer 15-Jan-13 7:58am    
Yes Exactly. Kindly let me know the code for stored procedure and C# code. My table name is "Register", Column Name is "Lecturetime", List Items are(1.Monday,2.Tuesday, 3.Wednesday, 4.Thursday, 5.Friday, 6.Saturday, 7.Sunday). Thanks in advance "Faisaladians".
AshishChaudha 15-Jan-13 7:34am    
my +5!
Faisalabadians 15-Jan-13 7:38am    
thanks AshishChaudha ...
The COUNT(*) function returns the number of records in a table.

SQL
SELECT COUNT(*) FROM table_name


Write down this statement in a stored procedure, assign the return value from the stored procedure to a variable in C#.
 
Share this answer
 
Comments
2012programmer 15-Jan-13 3:35am    
Thanks Vani Kulkarni for early response.Kindly let me know how to write a stored procedure. I am new to programming. Can any body tell complete code. Thanks in advance.
Hi,

Select count(*) from table_name will take too much time on large data. If you want it realy fast then you should use:

SELECT    Total_Rows= SUM(st.row_count) FROM sys.dm_db_partition_stats st WHERE object_name(object_id) = 'TableName' AND (index_id < 2)



RKS
 
Share this answer
 
SQL
DB side Part

Declare @TotalCnt Int

SELECT @TotalCnt = COUNT(*) FROM table_name

Select @TotalCnt AS TotalCount


On C# side Store the output in variable using command Execute scalar.
 
Share this answer
 
v2
C#
String lsSqlStr = "Select Count(ColumnName) From TableName"
SqlCommand lObjCmd = New SqlCommand(lsSqlStr con)
// con defines the connection string

SqlDataReader lObjDr = New SqlDataReader(); 
lObjDr = lObjCmd.ExecuteReader();

// Open DataReader to Read the Data
 lObjDr.Read();

int lsCountVal = Convert.ToInt32(lObjDr [0].ToString()); 

// Close DataReader
lObjDrClose();
lObjDr.Dispose();

// Close Connection
con .Close();
con Dispose();
 
Share this answer
 
v2
SQL
create proc countrows
as
begin

Declare @rowscount int

SELECT @rowscount= COUNT(*) FROM table_name
print @rowscount
end


Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900