C Amr Moneim
C Amr Moneim

Reputation: 41

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?

Upvotes: 0

Views: 123

Answers (2)

Michael Buckman
Michael Buckman

Reputation: 150

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;
reader.Execute(SQLCmd);

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()?

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222720

Just use name of the column begin returned from the database i.e "Client" here. If it is a string, you can use .ToString(). If it is another type, you need to convert it using System.Convert

string Value = reader["Client"].ToString();

Upvotes: 1

Related Questions