0

Here is the retrieval of the data:

string connectionString = ConfigurationManager.ConnectionStrings["CString"].ConnectionString;
// string connectionString = WebConfigurationManager.ConnectionStrings["CString"].ConnectionString;
string selectSQL = "SELECT Field1, Field2 FROM Table";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

Suppose I want the data output to a generic literal content holder exactly like so:

['Field1', Field2'],
['A', 11],
['B', 2],
['C', 2],
['D', 2],
['E', 7]

where Field1 and Field2 are the column names specified in the query. How do I get it to iterate over each returned result and return in this format?

2
  • 1
    haven't written c# lately, but try adapter.fill(ds); then do foreach(datarow row in ds.tables[0] Commented Feb 18, 2014 at 22:16
  • Was just gonna say same thing Commented Feb 18, 2014 at 22:31

3 Answers 3

2
StringBuilder strb = new StringBuilder();
foreach (DataColumn column in ds.Tables[0].Columns)
{
    strb.Append(column.ColumnName);
    strb.Append(",");
}
strb.Append("\n")

foreach (DataRow dr in ds.Tables[0].Rows)
{
    for (int i=0; i<dr.ItemArray.Length; i++)
    {
        strb.Append(dr[i].ToString());
        strb.Append(",");
    }
    strb.Append("\n")
}
Sign up to request clarification or add additional context in comments.

2 Comments

very close, but the format still does not match what I have and not sure how to enclose each data item in single quotes and group both fields in brackets using this.
the last item doesn't have a comma at the end, and for whatever fields given they need to be enclosed in brackets and each item individually in single quotes, like in the example shown. I don't know how to get this example to do that.
0
    System.Text.StringBuilder strb = new System.Text.StringBuilder();
    strb.Append("[");
    foreach (DataColumn column in ds.Tables[0].Columns)
    {
        strb.Append("'");
        strb.Append(column.ColumnName);
        strb.Append("', ");
    }
    strb.Append("],\n");

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        strb.Append("[");

        for (int i=0; i<dr.ItemArray.Length; i++)
        {
            if (i == (dr.ItemArray.Length - 1))
            {
                strb.Append(dr[i].ToString());
            }
            else
            {
                strb.Append("'");
                strb.Append(dr[i].ToString());
                strb.Append("', ");
            }
        }

        strb.Append("],\n");
    }

1 Comment

This is the expanded version of Eric's answer that works and meets what I needed.
0
   StringBuilder strb = new StringBuilder();
   DataColumn[] dca = new DataColumn[ds.Tables[0].Columns.Count];
   ds.Tables[0].Columns.CopyTo(dca, 0);

   string format = "['{0}'],\n";
   strb.AppendFormat(format, string.Join("','", dca.Select(c => c.ColumnName)));

   foreach (DataRow dr in ds.Tables[0].Rows)
      strb.AppendFormat (format, string.Join ("','", dr.ItemArray.Select (i => i.ToString ())));

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.