hi everyone,
How to count the inside the sql server using dataadapter...for ex...i hava a many blackdiamond name in my database....how to count how many blackdiamond inside my table....
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Aug 30, 2011, 6:33 AM
Iam Prabhu, not Abhishek .its ok.
Sql Server doesn't allow double quotes to define String.
use Single quotes to represent String.
Try following,
//import .Net Package to your Class
using System.Data;
using System.Linq;
using System.Data.SqlClient;
String ConnectionString = "Provide your Database Connection String Here"; // must provide connection string in double quotes
SqlConnection loadCon = new SqlConnection(ConnectionString);
String selQuery = "Select count(name) from tablename where tablename.name = 'blackdiamond'"; //change the query as you like
SqlCommand loadCom = new SqlCommand(selQuery);
int count=0;
try
{
loadCon.Open();
loadCom.Connection = loadCon;
SqlDataReader reader = loadCom.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
++count;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
loadCom.Parameters.Clear();
loadCom.Dispose();
loadCom = null;
loadCon.Close();
}
Black DiamondPosted Aug 30, 2011, 10:32 AM
Black DiamondPosted Aug 30, 2011, 4:02 AM
Thanks for the response, but i got an error in the code the underline is the error i got:
Select count(name) from tablename where tablename.name = "blackdiamond"
int count=0;
while (dataReaderobject.Read())
{
if(dataReaderObject["name"].ToString() == "blackdiamond")
{
++count;
}
}
Prabhu RajaPosted Aug 30, 2011, 3:23 AM
Abhishek BhatPosted Aug 30, 2011, 3:16 AM