0

This is my code below:

SqlConnection SqlConnections = new SqlConnection();
SqlConnections.ConnectionString = Global.con;
SqlCommand SqlCommands = new SqlCommand("SELECT Id,LinkedUserID From Users");
SqlConnections.Open();
SqlCommands.Connection = SqlConnections;
SqlDataReader SqlDataReaders =   SqlCommands.ExecuteReader(CommandBehavior.CloseConnection);
while (SqlDataReaders.Read())
{
   //
}
SqlDataReaders.Close();

Im trying to run look with this code for read user id and LinkedUserID I want to all both ids for make a efface on all users as per i want please let me know how can i read both ids and return to ever single id for make a affect in database. i want to do this for use in MLM web appls during new join under any old member so i can make a affect points other user easily. any one have a idea.

Thank You

4
  • Can you please state clearly what you want to achieve? I'm getting lost in stars. Commented Jan 3, 2014 at 13:09
  • sure i want to read all id and linked user ids with user why i want to use because when any user give a new joying at that case i want to change points of other all upper user on new joining Commented Jan 3, 2014 at 13:14
  • So you can fetch it as SqlDataReaders["Id"] and SqlDataReader["LinkedUserID"].You can write fetch code inside the while loop. Commented Jan 3, 2014 at 13:26
  • please give me example how can i do this.. Commented Jan 3, 2014 at 13:39

1 Answer 1

2

First you need a class to hold the data you're fetching:

public class LinkData
{
   public int Id { get; set; }
   public int LinkedUserId { get; set; }
}

Then, when you iterate over your SqlDataReader, create instances of that class and store them e.g. in a List<LinkData> variable:

List<LinkData> links = new List<LinkData>();

using (SqlConnection SqlConnections = new SqlConnection(Global.con))
using (SqlCommand SqlCommands = new SqlCommand("SELECT Id,LinkedUserID From Users", SqlConnections))
{
    SqlConnections.Open();

    using (SqlDataReader SqlDataReaders = SqlCommands.ExecuteReader(CommandBehavior.CloseConnection))
    {
        while (SqlDataReaders.Read())
        {
            LinkData newItem = new LinkData();
            newItem.Id = SqlDataReaders.GetInt32(0);
            newItem.LinkedUserId = SqlDataReaders.GetInt32(1);

            links.Add(newItem);
        }

        SqlDataReaders.Close();
   }

At the end, your Links list should contain all data fetched from the database.

This is just the absolute "bare bones" solution - in your real-world code, you should probably also add some check to protect your code from crashing if one of the values is NULL in the database.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello marc_s,how can i count links list is and return value.
I need to use loop for Links list?
If all you want to do is to retrieve data why not use DataAdapter and DataTable to store data.
@AshishCharan: using SqlDataReader is still a bit faster, and using a List<LinkData> has a lot less overhead than a DataSet or a DataTable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.