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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prabhu RajaRank #861 · 2.6K reputationPosted Aug 30, 2011, 2:33 AM EDT
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();
}
Abhishek BhatRank #828 · 2.7K reputationPosted Aug 29, 2011, 11:16 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Aug 29, 2011, 11:23 PM EDT
Black Diamond191 reputationPosted Aug 30, 2011, 12:02 AM EDT
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;
}
}
Black Diamond191 reputationPosted Aug 30, 2011, 6:32 AM EDT