如何在立即窗口中轻松查看数据表或数据视图的内容


87

有时我会在代码中出现断点,并且想查看DataTable变量的内容(或DataTable中的DataSet)。快速观看并不能使您清楚地看到其中的内容。我如何轻松查看它们?


您正在使用哪个版本的Visual Studio?
Gerrie Schenck

1
那么为什么不使用内置的可视化工具呢?
Gerrie Schenck,2009年

Answers:


171

Visual Studio调试器带有四个标准的可视化工具。这些是文本,HTML和XML可视化工具,它们全部适用于字符串对象,而数据集可视化工具,其适用于DataSet,DataView和DataTable对象。

要使用它,请插入代码,将鼠标悬停在DataSet上,展开快速查看,查看Tables,展开该表,然后查看Table [0](例如)。您会在快速手表中看到类似{Table1}的内容,但请注意,这里还有一个放大镜图标。单击该图标,您的数据表将在网格视图中打开。

在此处输入图片说明


15

为了美化adinas的调试器输出,我进行了一些简单的格式化:

    public void DebugTable(DataTable table)
    {
        Debug.WriteLine("--- DebugTable(" + table.TableName + ") ---");
        int zeilen = table.Rows.Count;
        int spalten = table.Columns.Count;

        // Header
        for (int i = 0; i < table.Columns.Count; i++)
        {
            string s = table.Columns[i].ToString();
            Debug.Write(String.Format("{0,-20} | ", s));
        }
        Debug.Write(Environment.NewLine);
        for (int i = 0; i < table.Columns.Count; i++)
        {
            Debug.Write("---------------------|-");
        }
        Debug.Write(Environment.NewLine);

        // Data
        for (int i = 0; i < zeilen; i++)
        {
            DataRow row = table.Rows[i];
            //Debug.WriteLine("{0} {1} ", row[0], row[1]);
            for (int j = 0; j < spalten; j++)
            {
                string s = row[j].ToString();
                if (s.Length > 20) s = s.Substring(0, 17) + "...";
                Debug.Write(String.Format("{0,-20} | ", s));
            }
            Debug.Write(Environment.NewLine);
        }
        for (int i = 0; i < table.Columns.Count; i++)
        {
            Debug.Write("---------------------|-");
        }
        Debug.Write(Environment.NewLine);
    }

最佳解决方案:您不需要Visual Studio!这是我的示例输出:

从verpackungen中选择PackKurz,PackName,PackGewicht

PackKurz | 包装名称| PackGewicht |
--------------------- | ---------------------- | ----- ----------------- |-
BB205 | 大袋205公斤| 205 |
BB300 | 大袋300公斤| 300 |
BB365 | 365公斤大袋| 365 |
CO | 集装箱,备用集装箱... | |
EP | 调色板| |
IBC | Chemikaliengefäß... | |
输| nicht包装... | 0 |
--------------------- | ---------------------- | ----- ----------------- |-

1

我要做的是在我的项目中有一个带有以下代码的静态类:

    #region Dataset -> Immediate Window
public static void printTbl(DataSet myDataset)
{
    printTbl(myDataset.Tables[0]);
}
public static void printTbl(DataTable mytable)
{
    for (int i = 0; i < mytable.Columns.Count; i++)
    {
        Debug.Write(mytable.Columns[i].ToString() + " | ");
    }
    Debug.Write(Environment.NewLine + "=======" + Environment.NewLine);
    for (int rrr = 0; rrr < mytable.Rows.Count; rrr++)
    {
        for (int ccc = 0; ccc < mytable.Columns.Count; ccc++)
        {
            Debug.Write(mytable.Rows[rrr][ccc] + " | ");
        }
        Debug.Write(Environment.NewLine);
    }
}
public static void ResponsePrintTbl(DataTable mytable)
{
    for (int i = 0; i < mytable.Columns.Count; i++)
    {
        HttpContext.Current.Response.Write(mytable.Columns[i].ToString() + " | ");
    }
    HttpContext.Current.Response.Write("<BR>" + "=======" + "<BR>");
    for (int rrr = 0; rrr < mytable.Rows.Count; rrr++)
    {
        for (int ccc = 0; ccc < mytable.Columns.Count; ccc++)
        {
            HttpContext.Current.Response.Write(mytable.Rows[rrr][ccc] + " | ");
        }
        HttpContext.Current.Response.Write("<BR>");
    }
}

public static void printTblRow(DataSet myDataset, int RowNum)
{
    printTblRow(myDataset.Tables[0], RowNum);
}
public static void printTblRow(DataTable mytable, int RowNum)
{
    for (int ccc = 0; ccc < mytable.Columns.Count; ccc++)
    {
        Debug.Write(mytable.Columns[ccc].ToString() + " : ");
        Debug.Write(mytable.Rows[RowNum][ccc]);
        Debug.Write(Environment.NewLine);
    }
}
#endregion

然后,我将在立即窗口中调用上述功能之一,结果也将显示在其中。例如,如果我想查看变量“ myDataset”的内容,我将调用printTbl(myDataset)。点击回车后,结果将被打印到立即窗口


1

给出的xml展示台一试。还没有尝试过最新版本,但是如果没有Visual Studio 2003中的先前版本,我将无法工作。

除了分层显示DataSet之外,还有许多其他方便的功能,例如过滤和选择要查看的RowState。


1
public static void DebugDataSet ( string msg, ref System.Data.DataSet ds )
{
    WriteIf ( "===================================================" + msg + " START " );
    if (ds != null)
    {
        WriteIf ( msg );
        foreach (System.Data.DataTable dt in ds.Tables)
        {
            WriteIf ( "================= My TableName is  " +
            dt.TableName + " ========================= START" );
            int colNumberInRow = 0;
            foreach (System.Data.DataColumn dc in dt.Columns)
            {
                System.Diagnostics.Debug.Write ( " | " );
                System.Diagnostics.Debug.Write ( " |" + colNumberInRow + "| " );
                System.Diagnostics.Debug.Write ( dc.ColumnName + " | " );
                colNumberInRow++;
            } //eof foreach (DataColumn dc in dt.Columns)
            int rowNum = 0;
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                System.Diagnostics.Debug.Write ( "\n row " + rowNum + " --- " );
                int colNumber = 0;
                foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    System.Diagnostics.Debug.Write ( " |" + colNumber + "| " );
                    System.Diagnostics.Debug.Write ( dr[dc].ToString () + " " );
                    colNumber++;
                } //eof foreach (DataColumn dc in dt.Columns)
                rowNum++;
            }   //eof foreach (DataRow dr in dt.Rows)
            System.Diagnostics.Debug.Write ( " \n" );
            WriteIf ( "================= Table " + dt.TableName + " ========================= END" );
            WriteIf ( "===================================================" + msg + " END " );
        }   //eof foreach (DataTable dt in ds.Tables)
    } //eof if ds !=null 
    else
    {
        WriteIf ( "NULL DataSet object passed for debugging !!!" );
    }
} //eof method 

public static void WriteIf ( string msg )
{
    //TODO: FIND OUT ABOUT e.Message + e.StackTrace from Bromberg eggcafe
int output = System.Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["DebugOutput"] );
    //0 - do not debug anything just run the code 
switch (output)
{
    //do not debug anything 
    case 0:
        msg = String.Empty;
    break;
        //1 - output to debug window in Visual Studio       
        case 1:
            System.Diagnostics.Debug.WriteIf ( System.Convert.ToBoolean( System.Configuration.ConfigurationSettings.AppSettings["Debugging"] ), DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n" );
            break;
        //2 -- output to the error label in the master 
        case 2:
            string previousMsg = System.Convert.ToString (System.Web.HttpContext.Current.Session["global.DebugMsg"]);
            System.Web.HttpContext.Current.Session["global.DebugMsg"] = previousMsg +
            DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n </br>";
            break;
        //output both to debug window and error label 
        case 3:
            string previousMsg1 = System.Convert.ToString (System.Web.HttpContext.Current.Session["global.DebugMsg"] );
            System.Web.HttpContext.Current.Session["global.DebugMsg"] = previousMsg1 + DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n";
            System.Diagnostics.Debug.WriteIf ( System.Convert.ToBoolean( System.Configuration.ConfigurationSettings.AppSettings["Debugging"] ), DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n </br>" );
            break;
        //TODO: implement case when debugging goes to database 
    } //eof switch 

} //eof method WriteIf

1

并且,如果您想在任何地方使用它...作为DataTable的帮助者,则假定您想将输出捕获到Log4Net,但是我尝试的出色的入门示例只是将其转储到控制台...这也有可编辑的列宽变量nMaxColWidth -最终,我将在任何情况下通过...

public static class Helpers
    {
        private static ILog Log = Global.Log ?? LogManager.GetLogger("MyLogger");
        /// <summary>
        /// Dump contents of a DataTable to the log
        /// </summary>
        /// <param name="table"></param>
        public static void DebugTable(this DataTable table)
        {
            Log?.Debug("--- DebugTable(" + table.TableName + ") ---");
            var nRows = table.Rows.Count;
            var nCols = table.Columns.Count;
            var nMaxColWidth = 32;

            // Column Headers

            var sColFormat = @"{0,-" + nMaxColWidth + @"} | ";
            var sLogMessage = string.Empty;
            for (var i = 0; i < table.Columns.Count; i++)
            {
                sLogMessage = string.Concat(sLogMessage, string.Format(sColFormat, table.Columns[i].ToString()));
            }
            //Debug.Write(Environment.NewLine);
            Log?.Debug(sLogMessage);

            var sUnderScore = string.Empty;
            var sDashes = string.Empty;
            for (var j = 0; j <= nMaxColWidth; j++)
            {
                sDashes = sDashes + "-";
            }


            for (var i = 0; i < table.Columns.Count; i++)
            {
                sUnderScore = string.Concat(sUnderScore, sDashes + "|-");
            }

            sUnderScore = sUnderScore.TrimEnd('-');

            //Debug.Write(Environment.NewLine);
            Log?.Debug(sUnderScore);

            // Data
            for (var i = 0; i < nRows; i++)
            {
                DataRow row = table.Rows[i];
                //Debug.WriteLine("{0} {1} ", row[0], row[1]);
                sLogMessage = string.Empty;

                for (var j = 0; j < nCols; j++)
                {
                    string s = row[j].ToString();
                    if (s.Length > nMaxColWidth) s = s.Substring(0, nMaxColWidth - 3) + "...";
                    sLogMessage = string.Concat(sLogMessage, string.Format(sColFormat, s));
                }

                Log?.Debug(sLogMessage);
                //Debug.Write(Environment.NewLine);
            }           
            Log?.Debug(sUnderScore);
        }
}

1

我为它编写了一个小方法..它的泛化功能..

    public static void printDataTable(DataTable tbl)
    {
        string line = "";
        foreach (DataColumn item in tbl.Columns)
        {
            line += item.ColumnName +"   ";
        }
        line += "\n";
        foreach (DataRow row in tbl.Rows)
        {
            for (int i = 0; i < tbl.Columns.Count; i++)
            {
                line += row[i].ToString() + "   ";
            }
            line += "\n";
        }
        Console.WriteLine(line) ;
    }

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.