0

I am new to SQL Server so sorry if my question seems trivial but I have not been able to find a good answer to this or at least one that I can utilize.

My problem is that I need to use sql datareader to get table GUIDs which I will use later to insert entries into another database. Closing the datareader disposes of all information inside of it and I have not been able to find how to return the datareader values so that I can call them later in my code. How do you return or store datareader values to be called upon later?

4
  • You have to keep the reader open while using the data or else you will need to store it in a temp location (again before closing it). You could also just use a datatable. Commented Jul 2, 2013 at 17:04
  • 2
    create a List<Guid> and add the Guids to that and return it, it'll be easier to work with Commented Jul 2, 2013 at 17:04
  • Please post your code Commented Jul 2, 2013 at 17:04
  • 1
    read them and put in any suitable container, like List<T>(any other generic collection or just array) Commented Jul 2, 2013 at 17:04

3 Answers 3

3

Rather than returning the data reader add all of the values to a collection. Here you'll want a List<T> or T[] (array). This should contain objects that model the results of your query. If you're only returning the Guids then it would be List<Guid> or Guid[].

In general, all of your db interactions should be in one layer of the application. It's at that point that you want to deserialize query results. Elsewhere in your code you should deal with native C# types rather than a query result that is still pending deserialization. Here you generally just want methods that take whatever data is necessary to form a query and return an object or a collection of objects which model the query results.

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

1 Comment

Thanks for the speedy help. I ended up using ebyrob's suggestion but your suggestion is more general and should help other readers.
0

You could just store it to a DataTable:

SqlDataReader dr = null; // something you already have
DataTable dt = new DataTable();
dt.Load(dr);

Now all the rows and columns should be available in the DataTable until you're done using it.

Note: DataTable lives at: System.Data.DataTable.

2 Comments

Thanks this one worked out the best for me, it helped me bypass a lot of the work that the other options needed. Although I think using a more general List<> option would apply to more users.
@JuanPortela Interesting that you notice this is useful but think a "more general" POCO solution would work for others. These 3 lines of code work for pretty much every SQL query with a result set. Dissecting and storing the result into lists of (probably user objects) requires custom code for each query (not to mention creating the storage types)
0

How I would do it (In the data-layer):

public List<Guid> GetAllIds()
{
    var cmd = new SqlCommand(yourConnectionString, "SELECT yourIDColumn from YourTable")

    var ids = new List<GUID>();
    Using(var reader = cmd.ExecuteReader())
    {
      while (reader.Read())
      {
        ids.Add(reader.GetGuid(0))
      }
    }
  return ids;
}

Comments

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.