How to convert all contents of a DataTable column to a delimited string in C #?

I am retrieving data from an MSSQL server using SqlDataAdapter and DataSet. From this DataSet, I create a DataTable. My goal is to convert each column of the table to a row where the elements are separated by a comma. I decided that I would try to convert the string first before getting the delimiter to work.

The code runs in code for the ASP.Net page. The ultimate goal is to pass the string to the jscript variable, this is a “functional requirement” that I create a column delimited row from the columns and that it should end as a jscript variable.

Here is what I still have:

    DataSet myDataSet = new DataSet();
    mySqlDataAdapter.Fill(myDataSet);
    DataTable temperature = myDataSet.Tables["Table"];

    // LOOP1
    foreach (DataRow row in temperature.Rows)
    // this loop works fine and outputs all elements
    // of the table to the web page, this is just to
    // test things out
    {
        foreach (DataColumn col in temperature.Columns)
        {
            Response.Write(row[col] + " ### ");
        }
        Response.Write("<br>");
    }

    // LOOP2
    foreach (DataColumn column in temperature.Columns)
    // this loop was meant to take all elements for each
    // column and create a string, then output that string
    {
        Response.Write(column.ToString() + "<br>");
    }

In LOOP1, everything works fine. My data has 4 columns, all of them are respectively displayed with one record per line on the web page.

LOOP2 http://msdn.microsoft.com/en-us/library/system.data.datacolumn.tostring.aspx, , , , , , , .

, LOOP2, 4 -. , . , , , DataColumn .toString. , . .

EDIT: SQL-, : @ImageShack

, , : "-6.7, -7, -7.2, -7.3, -7.3".

+5
3

", ":

foreach (var column in temperature.Columns)
{
    DataColumn dc = column as DataColumn;
    string s = string.Join(", ", temperature.Rows.OfType<DataRow>()
                                                 .Select(r => r[dc]));
    // do whatever you need with s now
}

, DataTable, :

DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Column #1"));
table.Columns.Add(new DataColumn("Column #2"));
table.Rows.Add(1, 2);
table.Rows.Add(11, 22);
table.Rows.Add(111, 222);

... "1, 11, 111" "2, 22, 222".


: , var, DataColumn, / ?

( , ):

// we decide we'll use results later, storing them temporarily here
List<IEnumerable<string>> columnsValues = new List<IEnumerable<string>>();
foreach (DataColumn column in temperature.Columns)
{
    var values = temperature.Rows.OfType<DataRow>()
                                 .Select(r => r[column].ToString())
    columnsValues.Add(values);
}

, . , , :

foreach (var lisOfValues in columnsValues)
{
    foreach (var value in listOfValues)
    {
        Debug.Write(value + " ");
    }

    Debug.WriteLine("");
}

1 11 111, 2 22 222. ?

Wrong.

2 22 222 . ? .Select(r => r[column].ToString()) column - , - , , , , last column.

, - , , this.

DataColumn foreach. , .Select(r => r[dc]) (, string.Join ), , - , , .

+8

, ; , ColumnName .

, . .

0

All Articles