How can I easily view the contents of a datatable or dataview in the immediate window

后端 未结 8 1579
情书的邮戳
情书的邮戳 2020-12-04 13:02

Sometimes I will be at a breakpoint in my code and I want to view the contents of a DataTable variable (or a DataTable in a DataSet).

8条回答
  •  青春惊慌失措
    2020-12-04 13:35

    What I do is have a static class with the following code in my project:

        #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("
    " + "=======" + "
    "); 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("
    "); } } 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

    I then I will call one of the above functions in the immediate window and the results will appear there as well. For example if I want to see the contents of a variable 'myDataset' I will call printTbl(myDataset). After hitting enter, the results will be printed to the immediate window

提交回复
热议问题