📄 sample4.cs
字号:
namespace apiBook
{
using System;
using System.Data;
public class TestClass
{
public static void Main()
{
TestClass test=new TestClass();
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.Int32"); testDC.ColumnName="sId";
testDT.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "Name";
testDC.Unique=true;
testDT.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "City";
testDT.Columns.Add(testDC); DataColumnCollection testDCC;
testDCC = testDT.Columns;
Console.WriteLine("表名:" + testDT.TableName);
test.DoPrint(testDCC);
Console.WriteLine();
Console.WriteLine("添加一列:");
testDCC.Add("Address");
//使用Add方法用指定名称和类型创建DataColumn对象,并将其添加到DataColumnCollection对象
test.DoPrint(testDCC);
Console.WriteLine();
Console.WriteLine("集合是否包含列sId?"+testDCC.Contains("sId"));
//使用Contains方法判断集合中是否包含该列
Console.WriteLine("列sId是否可以从集合中移除?"+testDCC.CanRemove(testDT.Columns[testDCC.IndexOf("sId")]));
//使用CanRemove方法判断该列能否被移除
Console.WriteLine("列sId的索引是:"+testDCC.IndexOf("sId"));
//使用IndexOf方法获取指定列的索引
Console.WriteLine("移除列sId后");
testDCC.Remove("sId");
//使用Remove方法删除列
Console.WriteLine("集合是否包含列sId?"+testDCC.Contains("sId"));
Console.WriteLine();
Console.WriteLine("当前集合的列:");
test.DoPrint(testDCC);
Console.ReadLine();
}
public void DoPrint(DataColumnCollection dCC)
{
foreach(DataColumn tempDC in dCC)
{
Console.Write("列名:"+tempDC.ColumnName);
Console.WriteLine(" ; 数据类型:"+
tempDC.DataType);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -