📄 sample6.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;
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.Int32"); testDC.ColumnName="Id";
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();
//使用NewRow方法获取DataRow对象
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);
testDR=testDT.NewRow();
testDR["Id"]=3;
testDR["Name"]="Tom ";
testDR["School"]="SCUT";
testDT.Rows.Add(testDR);
Console.WriteLine("当前表信息:");
test.DoPrint(testDT);
Console.WriteLine();
Console.WriteLine("添加一行信息");
testDR=testDT.NewRow();
testDR["Id"]=4;
testDR["Name"]="Cat ";
testDR["School"]="ZSU";
testDT.Rows.Add(testDR);
Console.WriteLine("添加行的状态: " +
testDR.RowState);
test.DoPrint(testDT);
testDR.AcceptChanges();
//使用AcceptChanges方法提交自上次调用
//AcceptChanges以来对该行进行的所有更改
Console.WriteLine("执行AcceptChanges操作该行状态:" + testDR.RowState);
test.DoPrint(testDT);
Console.WriteLine("再添加一行信息");
testDR=testDT.NewRow();
testDR["Id"]=5;
testDR["Name"]="Jim ";
testDR["School"]="ZSU";
testDT.Rows.Add(testDR);
Console.WriteLine("添加行的状态: " +
testDR.RowState);
test.DoPrint(testDT);
testDR.RejectChanges();
//使用RejectChanges方法拒绝自上次调用
//AcceptChanges以来对该行进行的所有更改
Console.WriteLine("执行RejectChanges操作该行状态:" + testDR.RowState);
test.DoPrint(testDT);
Console.WriteLine("开始编辑:");
foreach(DataRow dr in testDT.Rows)
{
dr.BeginEdit();
//使用BeginEdit方法对DataRow对象开始编辑操作
dr[0]=(int) dr[0]+10;
dr.EndEdit();
//使用EndEdit方法终止发生在该行的编辑
}
Console.WriteLine("编辑结束。");
test.DoPrint(testDT);
Console.WriteLine("设置错误信息:");
string errStr="这是错误的Id号";
testDR=testDT.Rows[0];
testDR.SetColumnError(testDT.Columns[0],errStr);
//使用SetColumnError方法为列设置错误说明
foreach(DataRow dre in testDT.Rows)
{
DataColumn[] dcA;
if(dre.HasErrors)
{
dcA = dre.GetColumnsInError();
//使用GetColumnsInError方法获取包含错误的列的数组
for(int i = 0; i < dcA.Length; i++)
{
Console.WriteLine("有错误信息的列名:"+dcA[i].ColumnName);
}
Console.WriteLine("清除所有错误信息");
dre.ClearErrors();
//使用ClearErrors方法清除该行错误
}
}
Console.WriteLine();
testDR=testDT.Rows[3];
testDR.Delete();
//使用Delete方法删除DataRow对象
testDR.AcceptChanges();
Console.WriteLine("删除最后一行:");
test.DoPrint(testDT);
DataTable testDTC=new DataTable("class");
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.Int32"); testDC.ColumnName="CId";
testDTC.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "Grade";
testDTC.Columns.Add(testDC);
string[] pstr={"Id"};
string[] cstr={"CId"};
DataRelation dR=new DataRelation("eq","student","class",pstr,cstr,true);
foreach(DataRelation drn in testDT.ChildRelations)
{
foreach(DataRow dr in testDT.Rows)
{
DataRow[] drA= dr.GetChildRows(drn);
//使用GetChildRows方法获取子级DataRow对象
for(int i = 0; i < drA.Length; i++)
{
foreach(DataColumn dc in testDT.Columns)
{
Console.WriteLine(drA[i][dc]);
}
}
}
}
testDR=testDT.Rows[0];
Console.WriteLine("第一列是否有空值?"+testDR.IsNull("Id"));
//使用IsNull方法判断该列是否有空值
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 + -