C Amr Moneim C Amr Moneim - 1 year ago 136
C# Question

How to use value from SQL Server in C#

I got value from SQL Server using this C# code:

SqlDataReader reader = new SqlCommand("select Top 1 Client From _ClientName group by Client order by count(*) desc", sqlCon.ShardDB).ExecuteReader();

How can I use this value again to insert it into another table?

Answer Source

First, for readability sake, try separating your code into separate lines, like so:

SQLReader reader;
SQLCommand SQLCmd = new SQLCommand();
SQLCmd.CommandText = "select Top 1 Client From _ClientName group by Client order by count(*) desc";
SQLCmd.Connection = sqlCon.SharedDB;

If I understood your comment to Sajeetharan's previous answer, you want to know how to advance to the next result if your query returns more than one. Have you tried SQLReader.Read()?


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download