use TSQL to assign value to a variable

Hi,  Expert

My database is using SQL server 2008; and I am using C#

I would like to select something from a table and then assign to a either string or integer varialbe

My plan is only select 2 values(rows) from a column . So I use "select top 2 [column_A] from table_X
how can I assign the return values to something like
int_1 = first value from above sql select
int_2 =  second value from above sql select

Could you please show me easy way and detail procedure from connection to DB to assign to assign value

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rmtogetherAuthor Commented:
it says C# is not available..
Alfred A.Commented:
You can also use a stored procedure to contain your SELECT statement and then you can use the following code to read your database as an example:

public bool GetSelectResult() {
        SqlClient.SqlDataReader DR;
        SqlClient.SqlConnection Conn = new SqlClient.SqlConnection(<your connection string>);
        SqlClient.SqlCommand Cmd = new SqlClient.SqlCommand("usp_GetSelectResult");
        try {
            if ((Conn.State != ConnectionState.Open)) {
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Connection = Conn;
            //Cmd.Parameters.AddWithValue("@Param1", Param1);  --This is needed if you have parameters.
            //Cmd.Parameters.AddWithValue("@Param2", Param2);
            DR = Cmd.ExecuteReader();
            while (DR.Read()) {
                // 'Read you data reader here.
                // you can exit while after two reads
            return true;
        catch (Exception ex) {
            _ErrorDescription = ("Error retrieving All Details: " 
                        + (ex.Message + (" - " 
                        + (ex.Source + (" - " + ex.StackTrace)))));
            return false;
        finally {
            if ((Conn.State == ConnectionState.Open)) {

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Click on the VB tab. VB tab shows both and C# code. The link has C# code go down the page you will find it
Alfred A.Commented:
Oh by the way, the

while (DR.Read()) {
                // 'Read you data reader here.
                // you can exit while after two reads

will automatically exit if there is nothing more to read.  If there are only two rows, then it will only read the two rows.
Here's some sample code using your straight tSQL
         // create an open the connection
         SqlConnection conn = 
            new SqlConnection("Data Source=DESKTOP;"
               + "Initial Catalog=Northwind;"
               + "Persist Security Info=True;"
               + "User ID=sa;Password=password");
         // create a SqlCommand object for this connection
         SqlCommand command = conn.CreateCommand();
         command.CommandText = "select top 2 [column_A] from table_X";
         command.CommandType = CommandType.Text;
         // execute the command that returns a SqlDataReader
         SqlDataReader reader = command.ExecuteReader();
         int int_1 = -1;
         int int_2 = -1;

         if (reader.Read()) {int_1 = reader["column_A"].ToString();}
         if (reader.Read()) {int_2 = reader["column_A"].ToString();}

         // *** use your variables here. int_1 and 2 are -1 if the data has less than 2 rows
         // close the connection

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.