Print Contents Of A DataTable

后端 未结 7 1834
再見小時候
再見小時候 2020-12-05 17:35

Currently I have code which looks up a database table through a SQL connection and inserts the top five rows into a Datatable (Table).

using(SqlCommand _cmd          


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 18:09

    Here is another solution which dumps the table to a comma separated string:

    using System.Data;
    
    public static string DumpDataTable(DataTable table)
            {
                string data = string.Empty;
                StringBuilder sb = new StringBuilder();
    
                if (null != table && null != table.Rows)
                {
                    foreach (DataRow dataRow in table.Rows)
                    {
                        foreach (var item in dataRow.ItemArray)
                        {
                            sb.Append(item);
                            sb.Append(',');
                        }
                        sb.AppendLine();
                    }
    
                    data = sb.ToString();
                }
                return data;
            }
    

提交回复
热议问题