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());
COUNT(*)
SELECT COUNT(*) FROM table_name
SELECT Total_Rows= SUM(st.row_count) FROM sys.dm_db_partition_stats st WHERE object_name(object_id) = 'TableName' AND (index_id < 2)
DB side Part Declare @TotalCnt Int SELECT @TotalCnt = COUNT(*) FROM table_name Select @TotalCnt AS TotalCount
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();
create proc countrows as begin Declare @rowscount int SELECT @rowscount= COUNT(*) FROM table_name print @rowscount end
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)