Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
191 views
in Technique[技术] by (31.9m points)

c# - Capturing count from an SQL query

What is the simplest way in C# (.cs file) to get the count from the SQL command

SELECT COUNT(*) FROM table_name

into an int variable?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

Use SqlCommand.ExecuteScalar() and cast it to an int:

cmd.CommandText = "SELECT COUNT(*) FROM table_name";
Int32 count = (Int32) cmd.ExecuteScalar();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
...