📄 sample7.cs
字号:
namespace apiBook
{
using System;
using System.Data;
public class TestDataRowClass
{
public static void Main()
{
TestDataRowClass test=new TestDataRowClass(); DataTable testDT=new DataTable("student");
DataColumn testDC;
DataColumn[] keys=new DataColumn[1];
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.Int32"); testDC.ColumnName="Id";
keys[0]=testDC;
testDT.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "Name";
testDT.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "School";
testDT.Columns.Add(testDC);
DataRow testDR=testDT.NewRow();
testDR["Id"]=1;
testDR["Name"]="Rose";
testDR["School"]="SCUT";
testDT.Rows.Add(testDR);
testDR=testDT.NewRow();
testDR["Id"]=2;
testDR["Name"]="Coke";
testDR["School"]="SCNU";
testDT.Rows.Add(testDR);
DataRowCollection testDRC;
testDRC = testDT.Rows;
testDR = testDT.NewRow();
testDR[0] = 3;
testDR[1] = "Mike";
testDR[2]="SCUT";
testDRC.Add(testDR);
//使用Add方法将DataRow对象添加到DataRowCollection中
test.DoPrint(testDT);
testDT.PrimaryKey=keys;
Console.WriteLine("是否有Id为1的记录?"+testDRC.Contains("1"));
//使用Contains方法判断集合中任何行的主键列中是否包含指定值
Console.WriteLine("是否有Id为5的记录?"+testDRC.Contains("5"));
testDR=testDRC.Find("2");
//使用Find方法查找指定的行
Console.WriteLine("Id为2的记录:");
foreach(DataColumn dc in testDT.Columns)
{
Console.Write(testDR[dc]+" ");
}
Console.WriteLine();
Console.WriteLine("插入前表的数据:");
test.DoPrint(testDT);
Console.WriteLine("插入后表的数据:");
testDR=testDT.NewRow();
testDR[0]=8;
testDR[1]="Nick";
testDR[2]="ZSU";
testDRC.InsertAt(testDR,1);
//使用InsertAt方法在指定位置插入行
test.DoPrint(testDT);
Console.WriteLine("删除第一行记录:");
testDRC.RemoveAt(0);
//使用RemoveAt方法删除指定行的记录
test.DoPrint(testDT);
Console.WriteLine("删除所有记录:");
testDRC.Clear();
//使用Clear方法清除所有行的集合
test.DoPrint(testDT);
Console.ReadLine();
}
public void DoPrint(DataTable tempDT)
{
Console.WriteLine(" "+tempDT.TableName+"表数据");
foreach(DataColumn dc in tempDT.Columns)
Console.Write(dc.ColumnName+" ");
Console.WriteLine();
foreach(DataRow dr in tempDT.Rows)
{
foreach(DataColumn dc in tempDT.Columns)
{
Console.Write(dr[dc]+" ");
}
Console.WriteLine();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -