就是在调试 程序 时,中断后,想要查看这时的一个DataTable中的内容,可是把这个DataTable加入到监视窗口中查看这个DataTable实在是困难,这个DataTable的内容可以一层层的打开,可是就是难以简洁而清楚的查看DataTable中的内容。 哪位有办法简洁而清楚的查看DataTable中的内容?
方法一:主要用于winForm,也可用于webForm,但显示的不太好看,测试过,绝对可用
using System;
using System.Data;
using System.IO;
using System.Text;
public class TableDebuger
{
static public string GetInfo(DataTable t)
{
DataRowCollection rows=t.Rows;
StringBuilder sb=new StringBuilder();
sb.Append("DataTable:").Append(t.TableName)
.Append("/nRows Count:").Append(rows.Count)
.Append("/n");
int l=rows.Count;
for(int i=0;i<l;i++)
{
DataRow row=rows[i];
sb.Append("/nRow:").Append(i).Append("/n");
foreach(DataColumn c in t.Columns)
{
sb.Append(c.ColumnName.PadRight(16,' ')).Append(row[c]).Append("/n");
}
}
return sb.ToString();
}
}
namespace LostinetSample
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
DataSet ds=new DataSet();
ds.ReadXml(new StringReader(
@"
<data>
<t>
<a>3</a>
<b>4</b>
</t>
<t>
<a>x</a>
<b>y</b>
</t>
</data>
"
),XmlReadMode.InferSchema);
//在下面设置断点.在命令窗口(Ctrl+Alt+I)里输入:TableDebuger.GetInfo(ds.Tables[0])/n
Console.Write("ok");
}
}
}
方法二:
/// <summary>
/// 自己写的调试datatable
/// </summary>
/// <param name="text"></param>
private void debugoutput(DataTable text)
{
int defaultrowwidth = 40;
string thisrow1 = "";
for (int c = 0; c <= text.Columns.Count - 1; c++)
{
thisrow1 +="["+ text.Columns[c].ColumnName.ToString()+"]";
for (int tt = 0; tt <= defaultrowwidth - (text.Columns[c].ColumnName.ToString().Length+2) - 1; tt++)
{
thisrow1 += " ";
}
}
System.Diagnostics.Debug.WriteLine(thisrow1);
for (int i = 0; i <= text.Rows.Count - 1; i++)
{
string thisrow = "";
for (int c = 0; c <= text.Columns.Count-1; c++)
{
thisrow += text.Rows[i][c].ToString();
for (int tt = 0; tt <= defaultrowwidth - text.Rows[i][c].ToString().Length - 1; tt++)
{
thisrow += " ";
}
}
System.Diagnostics.Debug.WriteLine(thisrow);
}
}