C# DataTable 在控制台中显示单行
C# DataTable Show Single Row in Console
我四处寻找一种方法来显示 C# 数据表的整行,既通过引用行号,也通过简单地将行内容写入字符串变量并在控制台中显示字符串。我可以指定确切的行和字段值并显示该值,但不能显示整行。这不是 C# 中的列表,这是数据表。
对于下面的简单代码,我得到的第一个 WriteLine 的输出是 "Horse",但是后两个 WriteLine 命令,我得到的是控制台输出 "System.Data.DataRow" 而不是整行数据.
我做错了什么?任何帮助将不胜感激。
using System;
using System.Data;
using System.Threading;
namespace DataTablePractice
{
class Program
{
static void Main(string[] args)
{
// Create a DataTable.
using (DataTable table = new DataTable())
{
// Two columns.
table.TableName = "table";
table.Columns.Add("Number", typeof(string));
table.Columns.Add("Pet", typeof(string));
// ... Add two rows.
table.Rows.Add("4", "Horse");
table.Rows.Add("10", "Moose");
// ... Display first field of the first row in the console
Console.WriteLine(table.Rows[0].Field<string>(1));
//...Display the first row of the table in the console
Console.WriteLine(table.Rows[0]);
//...Create a new row variable to add a third pet
var newrow = table.Rows.Add("15", "Snake");
string NewRowString = newrow.ToString();
//...Display the new row of data in the console
Console.WriteLine(NewRowString);
//...Sleep for a few seconds to examine output
Thread.Sleep(4000);
}
}
}
}
当你运行这个:
Console.WriteLine(table.Rows[0]);
实际上是这样调用的:
Console.WriteLine(table.Rows[0].ToString()); // prints object type, in this case a DataRow
如果是你自己的class,你可以override ToString to return whatever you need, but you don't have that option with the DataRow
class. And so it uses the default behavior as described here:
Default implementations of the Object.ToString method return the fully qualified name of the object's type.
您可以遍历列,例如:
var row = table.Rows[0];
for (var i = 0; i < row.Count; i++)
Console.Write(row[i] + " : ");
或者,将它们全部打印出来的更短方法:
Console.WriteLine(String.Join(" : ", table.Rows[0].ItemArray));
鉴于您的数据,也许您只想引用这两个字段?
foreach (DataRow row in dt.Rows)
Console.WriteLine($"You have {row[0]} {row[1]}(s).");
// You have 4 Horse(s).
// You have 10 Moose(s).
我四处寻找一种方法来显示 C# 数据表的整行,既通过引用行号,也通过简单地将行内容写入字符串变量并在控制台中显示字符串。我可以指定确切的行和字段值并显示该值,但不能显示整行。这不是 C# 中的列表,这是数据表。
对于下面的简单代码,我得到的第一个 WriteLine 的输出是 "Horse",但是后两个 WriteLine 命令,我得到的是控制台输出 "System.Data.DataRow" 而不是整行数据.
我做错了什么?任何帮助将不胜感激。
using System;
using System.Data;
using System.Threading;
namespace DataTablePractice
{
class Program
{
static void Main(string[] args)
{
// Create a DataTable.
using (DataTable table = new DataTable())
{
// Two columns.
table.TableName = "table";
table.Columns.Add("Number", typeof(string));
table.Columns.Add("Pet", typeof(string));
// ... Add two rows.
table.Rows.Add("4", "Horse");
table.Rows.Add("10", "Moose");
// ... Display first field of the first row in the console
Console.WriteLine(table.Rows[0].Field<string>(1));
//...Display the first row of the table in the console
Console.WriteLine(table.Rows[0]);
//...Create a new row variable to add a third pet
var newrow = table.Rows.Add("15", "Snake");
string NewRowString = newrow.ToString();
//...Display the new row of data in the console
Console.WriteLine(NewRowString);
//...Sleep for a few seconds to examine output
Thread.Sleep(4000);
}
}
}
}
当你运行这个:
Console.WriteLine(table.Rows[0]);
实际上是这样调用的:
Console.WriteLine(table.Rows[0].ToString()); // prints object type, in this case a DataRow
如果是你自己的class,你可以override ToString to return whatever you need, but you don't have that option with the DataRow
class. And so it uses the default behavior as described here:
Default implementations of the Object.ToString method return the fully qualified name of the object's type.
您可以遍历列,例如:
var row = table.Rows[0];
for (var i = 0; i < row.Count; i++)
Console.Write(row[i] + " : ");
或者,将它们全部打印出来的更短方法:
Console.WriteLine(String.Join(" : ", table.Rows[0].ItemArray));
鉴于您的数据,也许您只想引用这两个字段?
foreach (DataRow row in dt.Rows)
Console.WriteLine($"You have {row[0]} {row[1]}(s).");
// You have 4 Horse(s).
// You have 10 Moose(s).