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

rmtogetherAsked:
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.

SriVaddadiCommented:
0
rmtogetherAuthor Commented:
it says C# is not available..
0
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)) {
                Conn.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
            }
            DR.Close();
            return true;
        }
        catch (Exception ex) {
            _ErrorDescription = ("Error retrieving All Details: " 
                        + (ex.Message + (" - " 
                        + (ex.Source + (" - " + ex.StackTrace)))));
            return false;
        }
        finally {
            if ((Conn.State == ConnectionState.Open)) {
                Conn.Close();
            }
        }
    }
0

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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

SriVaddadiCommented:
Click on the VB tab. VB tab shows both vb.net and C# code. The link has C# code go down the page you will find it
0
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.
0
cyberkiwiCommented:
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");
 
         conn.Open();
 
         // 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
         reader.Close();
         conn.Close();

Open in new window

0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.