如何在调试时查看 DataTable

我刚开始使用 ADO.NET、数据集和数据表。我遇到的一个问题是,在尝试调试时,似乎很难判断数据表中的值是什么。

快速查看在 DataTable 中保存了哪些值的最简单方法是什么?是否有办法在调试时查看 VisualStudio 中的内容,或者是将数据写入文件的唯一选项?

我已经创建了一个小实用程序函数,它将把 DataTable 写到 CSV 文件中。然而,生成的 CSV 文件被切断了。在写一个系统的过程中,大约有3行本应该是最后一行的内容。引导文件刚刚停止。我不知道这是我的 CSV 转换方法的问题,还是 DataTable 的原始填充的问题。

更新

忘记最后一部分,我只是忘记冲洗我的流作家。

70342 次浏览

With a break point set, after the DataTable or DataSet is populated, you can see a magnifying glass if you hover over the variable. If you click on it, it will bring up the DataTable Visualizer, which you can read about here.

In this image you see below, dt is my DataTable variable and the breakpoint was hit a few lines below allowing me to hover over this value. Using Visual Studio 2008.

alt text

DataTable Visualizer (image credit):
alt text

I added two lines into my app inside a class named after the outermost class:

public MyClass()
{
// The following (2) lines are used for testing only.  Remove comments to debug.
System.Diagnostics.Debugger.Launch();
System.Diagnostics.Debugger.Break();
}

This should stop the app and bring it up in debug mode. Then you can step through it and look at the values in your objects as you hover over them.

set the break point on the dataset/datatable(f9 shortcut key for break point) and run your application (f5 is the shortcutkey ) When the break point comes mouse hover the dataset/datatable click on the glass shown in the hover image in visual studio .

Note : check compilation debug="true" is true in web config .Else visual studio wont go for debugging .

    /// <summary>
/// Dumps the passed DataSet obj for debugging as list of html tables
/// </summary>
/// <param name="msg"> the msg attached </param>
/// <param name="ds"> the DataSet object passed for Dumping </param>
/// <returns> the nice looking dump of the DataSet obj in html format</returns>
public static string DumpHtmlDs(string msg, ref System.Data.DataSet ds)
{
StringBuilder objStringBuilder = new StringBuilder();
objStringBuilder.AppendLine("<html><body>");


if (ds == null)
{
objStringBuilder.AppendLine("Null dataset passed ");
objStringBuilder.AppendLine("</html></body>");
WriteIf(objStringBuilder.ToString());
return objStringBuilder.ToString();
}


objStringBuilder.AppendLine("<p>" + msg + " START </p>");
if (ds != null)
{
if (ds.Tables == null)
{
objStringBuilder.AppendLine("ds.Tables == null ");
return objStringBuilder.ToString();
}




foreach (System.Data.DataTable dt in ds.Tables)
{


if (dt == null)
{
objStringBuilder.AppendLine("ds.Tables == null ");
continue;
}
objStringBuilder.AppendLine("<table>");


//objStringBuilder.AppendLine("================= My TableName is  " +
//dt.TableName + " ========================= START");
int colNumberInRow = 0;
objStringBuilder.Append("<tr><th>row number</th>");
foreach (System.Data.DataColumn dc in dt.Columns)
{
if (dc == null)
{
objStringBuilder.AppendLine("DataColumn is null ");
continue;
}




objStringBuilder.Append(" <th> |" + colNumberInRow.ToString() + " | ");
objStringBuilder.Append(  dc.ColumnName.ToString() + " </th> ");
colNumberInRow++;
} //eof foreach (DataColumn dc in dt.Columns)
objStringBuilder.Append("</tr>");


int rowNum = 0;
foreach (System.Data.DataRow dr in dt.Rows)
{
objStringBuilder.Append("<tr><td> row - | " + rowNum.ToString() + " | </td>");
int colNumber = 0;
foreach (System.Data.DataColumn dc in dt.Columns)
{
objStringBuilder.Append(" <td> |" + colNumber + "|" );
objStringBuilder.Append(dr[dc].ToString() + "  </td>");
colNumber++;
} //eof foreach (DataColumn dc in dt.Columns)
rowNum++;
objStringBuilder.AppendLine(" </tr>");
}   //eof foreach (DataRow dr in dt.Rows)


objStringBuilder.AppendLine("</table>");
objStringBuilder.AppendLine("<p>" + msg + " END </p>");
}   //eof foreach (DataTable dt in ds.Tables)


} //eof if ds !=null
else
{


objStringBuilder.AppendLine("NULL DataSet object passed for debugging !!!");
}
return objStringBuilder.ToString();


}