ฉันจะดูเนื้อหาของ datatable หรือ dataview ในหน้าต่างทันทีได้อย่างไร


88

บางครั้งฉันจะอยู่ที่จุดพักในโค้ดของฉันและฉันต้องการดูเนื้อหาของDataTableตัวแปร (หรือDataTableใน a DataSet) นาฬิกาด่วนไม่ได้ให้มุมมองที่ชัดเจนของเนื้อหา ฉันจะดูได้อย่างง่ายดายได้อย่างไร


คุณกำลังทำงานกับ Visual Studio เวอร์ชันใด
Gerrie Schenck

1
ทำไมไม่ใช้ Visualizer ในตัวล่ะ?
Gerrie Schenck

คำตอบ:


174

ดีบักเกอร์ Visual Studio มาพร้อมกับวิชวลไลเซอร์มาตรฐานสี่ตัว สิ่งเหล่านี้คือตัวสร้างภาพข้อความ HTML และ XML ซึ่งทั้งหมดนี้ทำงานกับออบเจ็กต์สตริงและตัวสร้างภาพข้อมูลซึ่งทำงานกับออบเจ็กต์ DataSet, DataView และ DataTable

ในการใช้งานให้เจาะรหัสของคุณวางเมาส์เหนือชุดข้อมูลขยายนาฬิกาด่วนดูตารางขยายสิ่งนั้นจากนั้นดูตาราง [0] (ตัวอย่าง) คุณจะเห็นบางอย่างเช่น {Table1} ในนาฬิกาด่วน แต่สังเกตว่ามีไอคอนรูปแว่นขยายด้วย คลิกที่ไอคอนนั้นและ DataTable ของคุณจะเปิดขึ้นในมุมมองแบบกริด

ป้อนคำอธิบายภาพที่นี่


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 ! นี่คือผลลัพธ์ตัวอย่างของฉัน:

เลือก PackKurz, PackName, PackGewicht จาก verpackungen

PackKurz | PackName | แพ็คเกวิชท์ |
--------------------- | ---------------------- | ----- ----------------- | -
BB205 | บิ๊กแบ็ก 205 กก. | 205 |
BB300 | บิ๊กแบ็ก 300 กก. | 300 |
BB365 | บิ๊กแบ็ก 365 กก. | 365 |
CO | ตู้คอนเทนเนอร์ Alteru ... | |
EP | จานสี | |
IBC | เคมิคาเลียนเกเฟ ... | |
แพ้ | nicht verpackungs ... | 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) หลังจากกดปุ่ม Enter ผลลัพธ์จะถูกพิมพ์ไปยังหน้าต่างทันที


1

ให้Xml Visualizerลอง ยังไม่ได้ลองใช้เวอร์ชันล่าสุด แต่ฉันไม่สามารถทำงานได้หากไม่มีเวอร์ชันก่อนหน้าใน Visual Studio 2003

นอกเหนือจากการแสดงชุดข้อมูลตามลำดับชั้นแล้วยังมีคุณสมบัติอื่น ๆ อีกมากมายเช่นการกรองและการเลือก 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) ;
    }

0

ฉันไม่ได้ลองด้วยตัวเอง แต่ Visual Studio 2005 (และใหม่กว่า) รองรับแนวคิดของ Debugger Visualizers สิ่งนี้ช่วยให้คุณกำหนดวิธีการแสดงวัตถุใน IDE ได้ อ่านบทความนี้เพื่อดูรายละเอียดเพิ่มเติม

http://davidhayden.com/blog/dave/archive/2005/12/26/2645.aspx

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.