C# neatly outputs DataTable in the console

Effect:

I. Introduction

On the Winform platform, controls such as DataGridView can be used to display the form data of the database, but in the C# console project, if the database query is useful, it is more difficult for us to see the effect of the query statement. For example, I write at will A console output, the code is as follows:

using System.Data;

namespace CSharpConnectMySQL
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string sql = "SELECT * FROM goods_type";
            DataSet dataSet = MySqlHelper.GetDataSet(sql);
            DataTable dt = dataSet.Tables[0];
            if(dt.Rows.Count > 0)
            {
                //打印所有列名
                string columnName = string.Empty;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    columnName += dt.Columns[i].ColumnName + " | ";
                }
                Console.WriteLine(columnName);
                Console.WriteLine("-------------------------");

                //打印每一行的数据
                foreach (DataRow row in dt.Rows)
                {
                    string columnStr = string.Empty;
                    foreach (DataColumn column in dt.Columns)
                    {
                        columnStr += row[column] + " | ";
                    }
                    Console.WriteLine(columnStr);
                }
            }

            Console.ReadKey();
        }
    }
}

Effect:

Query results in Navicat 16 for MySQL software

Since there is no automatic alignment function, the printed ones are particularly ugly, so I thought, can I write a method to print the database on the console, and it can be automatically aligned, although it is not very effective, at least it is comfortable to look at, then the next step Let's implement this function.

2. Realize the effect

Since the latter effect is useful to the database, if you have not installed mysql, you can install it and add some data, or manually write a DataTable entity and add some data, it is all possible.

Create a new C# console project, according to my previous post to add some classes needed to connect to the database, and the necessary plug-ins

C# connect to MySQL database_c# mysql_Xiong Siyu's Blog-CSDN Blog

Add a class ConsoleTool

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

public class ConsoleTool
{
    /// <summary>
    /// 在控制台打印 DataTable
    /// </summary>
    /// <param name="dt"></param>
    public static void PrintDataTabele(DataTable dt)
    {
        if (dt == null)
        {
            Console.WriteLine("DataTable 不能为空");
            return;
        }

        //获取每一列的最大长度
        float[] lenList = GetColumnsMaxFontLength(dt);
        if (IsExceedMaxValue(lenList))
        {
            Console.WriteLine("DataTable列的总长度太长,控制台无法完整显示");
            return;
        }

        //字符串末尾添加多少个空格
        int endAddEmptyCount = 2;
        //打印标题
        string columnName = string.Empty;

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            string value = dt.Columns[i].ColumnName;
            float emptyCount = lenList[i] - GetFontLenght(value);
            value = AppendEmpty(value, emptyCount + endAddEmptyCount);
            columnName += value;
        }
        string hengang = GetPrintBar(GetFontLenght(columnName));
        Console.WriteLine(hengang);
        Console.WriteLine(columnName);
        Console.WriteLine(hengang);

        //打印内容
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string columnStr = string.Empty;
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                string? value = dt.Rows[i][j].ToString();
                if (string.IsNullOrEmpty(value)) value = "null";
                float emptyCount = lenList[j] - GetFontLenght(value);
                value = AppendEmpty(value, emptyCount + endAddEmptyCount);
                columnStr += value;
            }
            Console.WriteLine(columnStr);
        }

        Console.WriteLine(hengang);
    }

    /// <summary>
    /// 是否超过了规定的最大值
    /// </summary>
    /// <param name="lenList"></param>
    /// <returns></returns>
    private static bool IsExceedMaxValue(float[] lenList)
    {
        int singleMax = 100;
        int totalLength = 90;
        float value = 0;
        for (int i = 0; i < lenList.Length; i++)
        {
            value += lenList[i];
        }
        if (value > totalLength)
        {
            return true;
        }
        for (int i = 0; i < lenList.Length; i++)
        {
            if (lenList[i] > singleMax)
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 获取列中字符串最大的长度
    /// </summary>
    /// <param name="dataTable"></param>
    /// <returns></returns>
    private static float[] GetColumnsMaxFontLength(DataTable dataTable)
    {
        if (dataTable.Columns.Count == 0) return new float[0];
        if (dataTable.Rows.Count == 0) return new float[0];

        float[] columnLength = new float[dataTable.Columns.Count];

        //先加标题
        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            columnLength[i] = GetFontLenght(dataTable.Columns[i].ColumnName);
        }

        //再加具体列
        for (int i = 0; i < dataTable.Rows.Count; i++)
        {
            for (int j = 0; j < dataTable.Columns.Count; j++)
            {
                string? columnsCon = dataTable.Rows[i][j].ToString();
                if (string.IsNullOrEmpty(columnsCon)) columnsCon = "null";
                float columnsConLen = GetFontLenght(columnsCon);
                if (columnsConLen > columnLength[j])
                {
                    columnLength[j] = columnsConLen;
                }
            }
        }

        return columnLength;
    }

    /// <summary>
    /// 获取字体的长度
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private static float GetFontLenght(string? value)
    {
        float nCount = 0;
        for (int i = 0; i < value?.Length; i++)
        {
            char val = value[i];
            //if (val >= 0x4e00 && val <= 0x9fbb)//汉字
            //    nCount += 1;
            if (IsChinese(val))
                nCount += 1;
            else
                nCount += 0.5f;
        }

        return nCount;
    }

    /// <summary>
    /// 给字符串添加空格
    /// </summary>
    /// <param name="value"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    private static string AppendEmpty(string? value, float count)
    {
        if (string.IsNullOrEmpty(value))
            return string.Empty;
        if (count == 0)
            return value + " ";

        string[] arr = count.ToString().Split('.');
        int integer = 0;
        int decimals = 0;
        if (arr.Length == 1)
        {
            if (!int.TryParse(arr[0], out integer))
                return string.Empty;
        }
        if (arr.Length == 2)
        {
            if (!int.TryParse(arr[0], out integer))
                return string.Empty;
            if (!int.TryParse(arr[1], out decimals))
                return string.Empty;
        }
        for (int i = 0; i < integer; i++)
        {
            value += "  ";
        }
        if (decimals == 5)
        {
            value += " ";
        }
        return value;
    }

    /// <summary>
    /// 获取横杆
    /// </summary>
    /// <param name="count"></param>
    /// <returns></returns>
    private static string GetPrintBar(float count)
    {
        string bar = string.Empty;
        string[] arr = count.ToString().Split('.');
        int integer = 0;
        int decimals = 0;
        if (arr.Length == 1)
        {
            if (!int.TryParse(arr[0], out integer))
                return string.Empty;
        }
        if (arr.Length == 2)
        {
            if (!int.TryParse(arr[0], out integer))
                return string.Empty;
            if (!int.TryParse(arr[1], out decimals))
                return string.Empty;
        }
        for (int i = 0; i < integer; i++)
        {
            bar += "--";
        }
        if (decimals == 5)
        {
            bar += "-";
        }

        return bar;
    }

    /// <summary>
    /// 是否是中文
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private static bool IsChinese(char value)
    {
        UnicodeEncoding unicodeencoding = new UnicodeEncoding();
        byte[] unicodebytearray = unicodeencoding.GetBytes(value.ToString());
        bool isChina = false;
        for (int i = 0; i < unicodebytearray.Length; i++)
        {
            i++;
            //如果是中文字符那么高位不为0  
            if (unicodebytearray[i] != 0)
            {
                isChina = true;
                break;
            }
        }
        return isChina;
    }
}

transfer:

using System.Data;

namespace CSharpConnectMySQL
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string sql = "SELECT * FROM goods_type";
            DataSet dataSet = MySqlHelper.GetDataSet(sql);
            if (dataSet.Tables.Count == 0) return;
            DataTable dt = dataSet.Tables[0];
            if(dt.Rows.Count > 0)
            {
                ConsoleTool.PrintDataTabele(dt);
            }

            Console.ReadKey();
        }
    }
}

run:

Since the data is relatively small, the effect is not obvious, and the effect will be better if there is more data.

end

Guess you like

Origin blog.csdn.net/qq_38693757/article/details/131533357