在控制台应用程序中使用DataSet?

时间:2015-11-12 00:49:17

标签: c# sql

我试图弄清楚如何呈现我(应该)从SQL查询获得的信息。这是我的代码:

        this.cmd = connection.CreateCommand();
        this.cmd.CommandText = "SELECT * FROM servers WHERE id > " + x + " AND id < " + y;
        this.adapter = new MySqlDataAdapter( this.cmd );
        this.adapter.SelectCommand = this.cmd;
        this.dataset = new DataSet();
        this.adapter.Fill( dataset );
        return this.dataset;

当我使用控制台应用程序时,如何使用this.dataset显示信息?谢谢

2 个答案:

答案 0 :(得分:0)

foreach (DataTable table in dataSet.Tables)
{
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            object item = row[column];
            // read column and item
            Console.WriteLine("item ", item );
        }
    }
}

答案 1 :(得分:0)

这是一个令人惊讶的(但直截了当的)问题。他们使用的集合类很难使用。我想出了一个基本的实现。它不会尝试在正确的列中排列行值,但输出比Juran更好。

主要答案在ToPrettyString()扩展方法中。由于框架中的缺点,大多数其他扩展方法只是为了让生活更轻松。

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var ds = new DataSet();
            var customersTable = ds.Tables.Add("Customers");
            customersTable.Columns.AddRange("FirstName", "LastName", "Id", "Address");
            customersTable.Rows.Add("Bob", "Sagget", 1, "123 Mockingbird Lane");
            customersTable.Rows.Add("John", "Doe", 2, "1600 Pennsylvanie Ave");
            customersTable.Rows.Add("Jane", "Doe", 3, "100 Main St");

            Console.WriteLine(ds.ToPrettyString());
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }

    static class ExtensionMethods
    {    
        public static string ToPrettyString(this DataSet ds)
        {
            var sb = new StringBuilder();
            foreach (var table in ds.Tables.ToList())
            {
                sb.AppendLine("--" + table.TableName + "--");
                sb.AppendLine(String.Join(" | ", table.Columns.ToList()));
                foreach (DataRow row in table.Rows)
                {
                    sb.AppendLine(String.Join(" | ", row.ItemArray));
                }
                sb.AppendLine();
            }
            return sb.ToString();
        }

        public static void AddRange(this DataColumnCollection collection, params string[] columns)
        {
            foreach (var column in columns)
            {
                collection.Add(column);
            }
        }       

        public static List<DataTable> ToList(this DataTableCollection collection)
        {
            var list = new List<DataTable>();
            foreach (var table in collection)
            {
                list.Add((DataTable)table);
            }
            return list;
        }

        public static List<DataColumn> ToList(this DataColumnCollection collection)
        {
            var list = new List<DataColumn>();
            foreach (var column in collection)
            {
                list.Add((DataColumn)column);
            }
            return list;
        }
    }
}

输出:

  

- 客户 -

     

FirstName |姓氏| Id |地址

     鲍勃| Sagget | 1 | 123 Mockingbird Lane

     

约翰| Doe | 2 | 1600 Pennsylvanie Ave

     

简| Doe | 3 | 100 Main St