Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
how can i store values of sql query in variable
Posted

1 solution

You can use ExecuteScalar method of SqlCommand class. Sample code is:

C#
SqlConnection connection = new SqlConnection(myConnectionString);
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT count(EmpID) FROM Employee";
int result = ((int)cmd.ExecuteScalar());
connection.Close();
 
Share this answer
 
Comments
Aniket Yadav 13-Feb-12 0:03am    
What You Want To Do Here Exactly?
Bhags1992 13-Feb-12 1:09am    
im using select accessories from ACCESORY where bday='y'; this query so i want to retrieve output of this query

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