[C#] 常用工具类——直接在浏览器输出数据

/// <summary>
    /// <para> </para>
    ///  常用工具类——直接在浏览器输出数据
    /// <para> -------------------------------------------------------------</para>
    /// <para> DumpDataTable:接在浏览器输出数据DataTable</para>
    /// <para> DumpListItemILIST:直接在浏览器输出数据ILIST<ListItem></para>
    /// <para> DumpDataTableILIST:直接在浏览器输出数据DataTableILIST</para>
    /// <para> DumpIntArrILIST:直接在浏览器输出数据整型数组ILIST<int[]></para>
    /// <para> DumpStrArrILIST:直接在浏览器输出数据字符串数组的ILIST<string[]></para>
    /// <para> DumpDataSet:直接在浏览器输出数据DataSet对象</para>
    /// <para> DumpObjectArr2:直接在浏览器中输出二维数组</para>
    /// </summary>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Resources;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace Utils
{
    /// <summary>
    /// <para> </para>
    ///  常用工具类——直接在浏览器输出数据
    /// <para> -------------------------------------------------------------</para>
    /// <para> DumpDataTable:接在浏览器输出数据DataTable</para>
    /// <para> DumpListItemILIST:直接在浏览器输出数据ILIST<ListItem></para>
    /// <para> DumpDataTableILIST:直接在浏览器输出数据DataTableILIST</para>
    /// <para> DumpIntArrILIST:直接在浏览器输出数据整型数组ILIST<int[]></para>
    /// <para> DumpStrArrILIST:直接在浏览器输出数据字符串数组的ILIST<string[]></para>
    /// <para> DumpDataSet:直接在浏览器输出数据DataSet对象</para>
    /// <para> DumpObjectArr2:直接在浏览器中输出二维数组</para>
    /// </summary>

    public class DumpHelper
    {
        #region 构造方法设置
        /// <summary>
        /// 数据类型颜色
        /// </summary>
        protected static string ColorDataType = "red";
        /// <summary>
        /// 行数颜色
        /// </summary>
        protected static string ColorRowCount = "red";
        /// <summary>
        /// 列数颜色
        /// </summary>
        protected static string ColorColumnsCount = "red";
        /// <summary>
        /// 无数据时颜色
        /// </summary>
        protected static string ColorNoData = "red";
        /// <summary>
        /// 序号颜色
        /// </summary>
        protected static string ColorSerial = "black";
        /// <summary>
        /// 字段名颜色
        /// </summary>
        protected static string ColorFieldName = "blue";
        /// <summary>
        /// 字段类型颜色
        /// </summary>
        protected static string ColorFieldType = "#cc9999";
        /// <summary>
        /// 字段值颜色
        /// </summary>
        protected static string ColorFieldValue = "#9933ff";
        /// <summary>
        /// 最终输出字符串
        /// </summary>
        protected static string DumpStr = string.Empty;
        /// <summary>
        /// 输出字符串DIV开始
        /// </summary>
        protected static string DivRef = "<div style=‘font-size:14px;line-height:20px;margin:5px 0 0 5px;‘>";
        #endregion

        #region  直接在浏览器输出数据DataTable
        /// <summary>
        /// 直接在浏览器输出数据DataTable
        /// </summary>
        /// <param name="Dt">DataTable表格</param>
        public static void DumpDataTable(DataTable Dt)
        {
            if (Dt != null)
            {
                int DtRowsCount = Dt.Rows.Count;
                int DtColumnsCount = Dt.Columns.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">TableDate</font> [ 表名:" + Dt.TableName + " 行数:<font color=" + ColorRowCount + ">" + DtRowsCount + "</font>,列数:<font color=" + ColorColumnsCount + ">" + DtColumnsCount + "</font> ] <hr>";
                if (DtRowsCount < 1) DumpStr += " <font color=" + ColorNoData + ">无表格行数据!</font>";
                else
                {
                    for (int i = 0; i < DtRowsCount; i++)
                    {
                        DumpStr += "( Row:<font color=" + ColorSerial + ">" + i + "</font> ) => ";
                        for (int j = 0; j < DtColumnsCount; j++)
                        {
                            string ColumnName = Dt.Columns[j].ColumnName.ToString();
                            string ColumnType = Dt.Columns[j].DataType.ToString();
                            DumpStr += "  [" + j + "] <font color=" + ColorFieldName + ">" + ColumnName + "</font> ( <font color=" + ColorFieldType + ">" + ColumnType + "</font> ) => \"<font color=" + ColorFieldValue + ">" + Dt.Rows[i][ColumnName].ToString() + "</font>\"";
                        }
                        DumpStr += "<p>";
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        #region 直接在浏览器输出数据ILIST<ListItem>
        /// <summary>
        /// 直接在浏览器输出数据ILIST<ListItem>
        /// </summary>
        /// <param name="ListItems">ILIST<ListItem>泛型数据</param>
        public static void DumpListItemILIST(IList<ListItem> ListItems)
        {
            if (ListItems != null)
            {
                int ListCount = ListItems.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<ListItem></font> [ 项目数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        DumpStr += "( " + i + " ) => ";
                        string Texts = ListItems[i].Text.ToString();
                        string Values = ListItems[i].Value.ToString();
                        DumpStr += "  <font color=" + ColorFieldName + ">[ Text ]</font> => \"<font color=" + ColorFieldValue + ">" + Texts + "</font>\"";
                        DumpStr += "  <font color=" + ColorFieldName + ">[ Value ]</font> => \"<font color=" + ColorFieldValue + ">" + Values + "</font>\"";
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        #region  直接在浏览器输出数据DataTableILIST
        /// <summary>
        /// 直接在浏览器输出数据DataTableILIST
        /// </summary>
        /// <param name="ListDataTable">ILIST<ListDataTable>泛型数据</param>
        public static void DumpDataTableILIST(IList<DataTable> ListDataTable)
        {
            if (ListDataTable != null)
            {
                int ListCount = ListDataTable.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<DataTable></font> [ DataTable数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        if (i > 0) DumpStr += "<hr>";
                        DataTable Dt = ListDataTable[i];
                        int DtRowsCount = Dt.Rows.Count;
                        int DtColumnsCount = Dt.Columns.Count;
                        DumpStr += "( 表序号:" + i + " 表名:" + Dt.TableName + " ) => [ 行数:<font color=" + ColorRowCount + ">" + DtRowsCount + "</font>,列数:<font color=" + ColorColumnsCount + ">" + DtColumnsCount + "</font> ]";
                        if (DtRowsCount < 1) DumpStr += " <font color=" + ColorNoData + ">表格中无数据!</font><p>";
                        else
                        {
                            for (int k = 0; k < DtRowsCount; k++)
                            {
                                DumpStr += "  ( Row:<font color=" + ColorSerial + ">" + k + "</font> ) => ";
                                for (int j = 0; j < DtRowsCount; j++)
                                {
                                    string ColumnName = Dt.Columns[j].ColumnName.ToString();
                                    string ColumnType = Dt.Columns[j].DataType.ToString();
                                    DumpStr += "    [" + j + "] <font color=" + ColorFieldName + ">" + ColumnName + "</font> ( <font color=" + ColorFieldType + ">" + ColumnType + "</font> ) => \"<font color=" + ColorFieldValue + ">" + Dt.Rows[k][ColumnName].ToString() + "</font>\"";
                                }
                                DumpStr += "<p>";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        #region 直接在浏览器输出数据整型数组ILIST<int[]>
        /// <summary>
        ///  直接在浏览器输出数据整型数组ILIST<int[]>
        /// </summary>
        /// <param name="IntList">ILIST<Int[]>泛型数据</param>
        public static void DumpIntArrILIST(IList<int[]> IntList)
        {
            if (IntList != null)
            {
                int ListCount = IntList.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<Int[]></font> [ Int[]个数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        int[] IntArr = IntList[i];
                        int IntArrCount = IntArr.Length;
                        DumpStr += "( Int[]:<font color=" + ColorSerial + ">" + i + "</font> ) => 数组元素个数:<font color=" + ColorSerial + ">" + IntArrCount + "</font>";
                        if (IntArrCount < 1) DumpStr += "  <font color=" + ColorNoData + ">数组中无数据</font><p>";
                        else
                        {
                            for (int j = 0; j < IntArrCount; j++)
                            {
                                DumpStr += "  [" + j + "]  ( <font color=" + ColorFieldType + ">Int</font> ) => \"<font color=" + ColorFieldValue + ">" + IntArr[j].ToString() + "</font>\"";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        #region 直接在浏览器输出数据字符串数组的ILIST<string[]>
        /// <summary>
        /// 直接在浏览器输出数据字符串数组的ILIST<string[]>
        /// </summary>
        /// <param name="StrList">ILIST<String[]>泛型数据</param>
        public static void DumpStrArrILIST(IList<string[]> StrList)
        {
            if (StrList != null)
            {
                int ListCount = StrList.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<String[]></font> [ String[]个数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        string[] StrArr = StrList[i];
                        int StrArrCount = StrArr.Length;
                        DumpStr += "( String[]:<font color=" + ColorSerial + ">" + i + "</font> ) => 数组元素个数:<font color=" + ColorSerial + ">" + StrArrCount + "</font>";
                        if (StrArrCount < 1) DumpStr += "  <font color=" + ColorNoData + ">数组中无数据</font><p>";
                        else
                        {
                            for (int j = 0; j < StrArrCount; j++)
                            {
                                DumpStr += "  [" + j + "]  ( <font color=" + ColorFieldType + ">Int</font> ) => \"<font color=" + ColorFieldValue + ">" + StrArr[j].ToString() + "</font>\"";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        #region 直接在浏览器输出数据DataSet对象
        /// <summary>
        /// 直接在浏览器输出数据DataSet对象
        /// </summary>
        /// <param name="DS">DataSet对象</param>
        public static void DumpDataSet(DataSet DS)
        {
            if (DS != null)
            {
                int DtCount = DS.Tables.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">DataSet对象</font> [ DataSet中表格个数:<font color=" + ColorRowCount + ">" + DtCount + "</font> ] <hr>";
                if (DtCount < 1) DumpStr += " <font color=" + ColorNoData + ">DataSet对象中无表格!</font>";
                else
                {
                    for (int i = 0; i < DtCount; i++)
                    {
                        if (i > 0) DumpStr += "<hr>";
                        DataTable Dt = DS.Tables[i];
                        int DtRowsCount = Dt.Rows.Count;
                        int DtColumnsCount = Dt.Columns.Count;
                        DumpStr += "( 表序号:" + i + " 表名:" + Dt.TableName + " ) => [ 行数:<font color=" + ColorRowCount + ">" + DtRowsCount + "</font>,列数:<font color=" + ColorColumnsCount + ">" + DtColumnsCount + "</font> ]";
                        if (DtRowsCount < 1) DumpStr += " <font color=" + ColorNoData + ">表格中无数据!</font><p>";
                        else
                        {
                            for (int k = 0; k < DtRowsCount; k++)
                            {
                                DumpStr += "  ( Row:<font color=" + ColorSerial + ">" + k + "</font> ) => ";
                                for (int j = 0; j < DtColumnsCount; j++)
                                {
                                    string ColumnName = Dt.Columns[j].ColumnName.ToString();
                                    string ColumnType = Dt.Columns[j].DataType.ToString();
                                    DumpStr += "    [" + j + "] <font color=" + ColorFieldName + ">" + ColumnName + "</font> ( <font color=" + ColorFieldType + ">" + ColumnType + "</font> ) => \"<font color=" + ColorFieldValue + ">" + Dt.Rows[k][ColumnName].ToString() + "</font>\"";
                                }
                                DumpStr += "<p>";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        #region 直接在浏览器中输出二维数组
        /// <summary>
        /// 直接在浏览器中输出二维数组
        /// </summary>
        /// <param name="ObjArr">二维数组</param>
        public static void DumpObjectArr2(object[] ObjArr)
        {
            if (ObjArr != null)
            {
                int ObjLength = ObjArr.Length;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">Object数组</font> [ 数组中元素个数:<font color=" + ColorRowCount + ">" + ObjLength + "</font> ] <hr>";
                for (int i = 0; i < ObjLength; i++)
                {
                    DumpStr += "  ( <font color=" + ColorSerial + ">" + i + "</font> ) ";
                    object Arri = ObjArr[i];
                    DumpStr += " ( ";
                    DumpStr += "<font color=" + ColorFieldType + ">" + Arri.GetType().ToString() + "</font>";
                    DumpStr += " ) ";
                    DumpStr += " => <font color=" + ColorFieldValue + ">\"" + Arri + "\"</font>";
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion

        public static void DumpObjectArr(object[] ObjArr)
        {
            if (ObjArr != null)
            {
                HttpContext.Current.Response.Write(ObjArr.GetType());
            }
        }
    }
}

  

[C#] 常用工具类——直接在浏览器输出数据,布布扣,bubuko.com

时间: 2024-08-05 11:14:02

[C#] 常用工具类——直接在浏览器输出数据的相关文章

Java常用工具类集合

数据库连接工具类 仅仅获得连接对象 ConnDB.java package com.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接工具类——仅仅获得连接对象 * */ public class ConnDB { private static Connection conn = null; private static final String DRIVER_NAME = "com.mysql

[C#] 常用工具类——文件操作类

/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在</para> /// <para> IsImgFilename:判断文件名是否为浏览器可以直接显示的图片文件名</para> /// <para> CopyFiles:复制指定目录的所有文件</para> /// <para> MoveFi

commons-lang常用工具类StringEscapeUtils使用

在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止sql注入,xss注入攻击的功能. commons-lang常用工具类StringEscapeUtils使用 - wjoygz - pauls private zone 1.escapeSql 提供sql转移功能,防止sql注入攻击,例如典型的万能密码攻击' ' or 1=1 ' ' 1StringBuffer sql = new StringBuffer("select key_sn,

【转载】Android应用框架及常用工具类总结

转载自:Android应用框架 http://www.tuicool.com/articles/feqmQj 常用工具类总结    http://blog.csdn.net/krislight/article/details/11354119 一. UML类图复习: UML类图中有些标记很容易混淆,这里先复习下,请大家看下面这幅图: 注:这幅图来自<大话设计模式>这本书中的插图. 二.应用框架: A.基本概念 抽象(抽出共同之现象)——在同领域的程序中,常含有许多类别,这些类别有其共同点,我们

php常用工具类

<?php /** * 常用工具类 * @author wj * @date   2015-4-15 */ class Tool { /** * js 弹窗并且跳转 * @param string $_info * @param string $_url * @return js */ static public function alertLocation($_info, $_url) { echo "<script type='text/javascript'>alert(

javascript 总结(常用工具类的封装,转)

javascript 总结(常用工具类的封装) 前言 因为工作中经常用到这些方法,所有便把这些方法进行了总结. JavaScript 1. type 类型判断 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } isNumber (o) { //是否数字 return Object.prototype.toString.call(o).slice(8, -1) ==

简单了解Spring中常用工具类_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名.URL 地址以及资源内容的操作方法 访问文件资源 * 通过 FileSystemResource 以文件系统绝对路径的

Android 常用工具类之SPUtil,可以修改默认sp文件的路径

参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; import android.content.SharedPreferences; import java.io.

js常用工具类.

一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * 使用方式一: * var now = new Date(); * var nowStr = now.dateFormat("yyyy-MM-dd hh:mm:ss"); * 使用方式二: * new Date().dateFormat("yyyy年MM月dd日");