Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SqlDataReader and Comma Seperated Values

Status
Not open for further replies.

spons

Programmer
Mar 17, 2004
18
US
Is there an easy way to Retrive Data With A DataReader and Convert it into Comma Seperated Values? I remember reading about something like this before and now I need to use it.
 
Could try this,
string sep = ", ";
StringBuilder sb = new StringBuilder();
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; ++i)
{
sb = sb.Append(dr.GetValue(i));
sb = sb.Append(sep);
}
}
sb.Remove(sb.Length - 2,2); //remove the trailing comma
string str = sb.ToString();
Marty
 
Another solution is for each successfull read that will return the columns with their values and types,
use string.Join() to concatenate the columns values as strings using a given separator.
Code:
myReader =new SqlDataReader();
string sep = ";"
Array myTargetArray=Array.CreateInstance( typeof(String), myReader.FieldCount );
for (int i=0;i<myReader.FiledCount;i++)
  myTargetArray.Add(myReader[i],i);
string sCommasep = String.Join(sep, myTargetArray);
-obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top