在项目中经常用到DataTable如果DataTable使用得当不仅能使程序简洁实用而且能够提高性能达到事半功倍的效果现对DataTable的使用技巧进行一下总结
一DataTable简介
()构造函数
DataTable() 不带参数初始化DataTable 类的新实例
DataTable(string tableName) 用指定的表名初始化DataTable 类的新实例
DataTable(string tableName string tableNamespace) 用指定的表名和命名空间初始化DataTable 类的新实例
() 常用属性
CaseSensitive 指示表中的字符串比较是否区分大小写
ChildRelations 获取此DataTable 的子关系的集合
Columns 获取属于该表的列的集合
Constraints 获取由该表维护的约束的集合
DataSet 获取此表所属的DataSetDataSet相关信息可见我以前的一篇文章《数据访问()-DataSet》
DefaultView 获取可能包括筛选视图或游标位置的表的自定义视图
HasErrors 获取一个值该值指示该表所属的DataSet 的任何表的任何行中是否有错误
MinimumCapacity 获取或设置该表最初的起始大小该表中行的最初起始大小默认值为
Rows 获取属于该表的行的集合
TableName 获取或设置DataTable 的名称
()常用方法
AcceptChanges() 提交自上次调用AcceptChanges() 以来对该表进行的所有更改
BeginInit() 开始初始化在窗体上使用或由另一个组件使用的DataTable初始化发生在运行时
Clear() 清除所有数据的DataTable
Clone() 克隆DataTable 的结构包括所有DataTable 架构和约束
EndInit() 结束在窗体上使用或由另一个组件使用的DataTable 的初始化初始化发生在运行时
ImportRow(DataRow row) 将DataRow 复制到DataTable 中保留任何属性设置以及初始值和当前值
Merge(DataTable table) 将指定的DataTable 与当前的DataTable 合并
NewRow() 创建与该表具有相同架构的新DataRow
二DataTable使用技巧
()Create a DataTable
DataTable dt = new DataTable(Table_AX);
()Add columns for DataTable
//Method
dtColumnsAdd(column SystemTypeGetType(SystemString));
//Method
DataColumn dc = new DataColumn(column SystemTypeGetType(SystemBoolean));
dtColumnsAdd(dc);
()Add rows for DataTable
//Initialize the row
DataRow dr = dtNewRow();
dr[column] = AX;
dr[column] = true;
dtRowsAdd(dr);
//Doesnt initialize the row
DataRow dr = dtNewRow();
dtRowsAdd(dr);
()Select row
//Search the second row 如果没有赋值则用is null来select
DataRow[] drs = dtSelect(column is null);
DataRow[] drss = dtSelect(column = AX);
()Copy DataTable include data
DataTable dtNew = dtCopy();
()Copy DataTable only scheme
DataTable dtOnlyScheme = dtClone();
()Operate one row
//对dt的操作
//Method
DataRow drOperate = dtRows[];
drOperate[column] = AXzhz;
drOperate[column] = false;
//Method
drOperate[] = AXzhz;
drOperate[] = false;
//Method
dtRows[][column] = AXzhz;
dtRows[][column] = false;
//Method
dtRows[][] = AXzhz;
dtRows[][] = false;
()Evaluate another DataTables row to current Datatable
dtOnlySchemeRowsAdd(dtRows[]ItemArray);
()Convert to string
SystemIOStringWriter sw = new SystemIOStringWriter();
SystemXmlXmlTextWriter xw = new SystemXmlXmlTextWriter(sw);
dtWriteXml(xw);
string s = swToString();
()Filter DataTable
dtDefaultViewRowFilter = column <> true;
dtDefaultViewRowFilter = column = true;
()Sort row
dtDefaultViewSort = ID Name ASC;
dt=dtDefaultViewToTable();
()Bind DataTable
//绑定的其实是DefaultView
gvTestDataTableDataSource = dt;
gvTestDataTableDataBind();
()judge the DataTables Column name is a string
//判断一个字符串是否为DataTable的列名
dtInfoColumnsContains(AX);
()DataTable convert to XML and XML convert to DataTable
protected void Page_Load(object sender EventArgs e)
{
DataTable dt_AX = new DataTable();
//dt_AXColumnsAdd(Sex typeof(SystemBoolean));
//DataRow dr = dt_AXNewRow();
//dr[Sex] = true;
//dt_AXRowsAdd(dr);
string xml=ConvertBetweenDataTableAndXML_AX(dt_AX);
DataTable dt = ConvertBetweenDataTableAndXML_AX(xml);
}
public string ConvertBetweenDataTableAndXML_AX(DataTable dtNeedCoveret)
{
SystemIOTextWriter tw = new SystemIOStringWriter();
//if TableName is empty WriteXml() will throw Exception
dtNeedCoveretTableName=dtNeedCoveretTableNameLength==?Table_AX:dtNeedCoveretTableName;
dtNeedCoveretWriteXml(tw);
dtNeedCoveretWriteXmlSchema(tw);
return twToString();
}
public DataTable ConvertBetweenDataTableAndXML_AX(string xml)
{
SystemIOTextReader trDataTable = new SystemIOStringReader(xmlSubstring( xmlIndexOf(<?xml)));
SystemIOTextReader trSchema = new SystemIOStringReader(xmlSubstring(xmlIndexOf(<?xml)));
DataTable dtReturn = new DataTable();
dtReturnReadXmlSchema(trSchema);
dtReturnReadXml(trDataTable);
return dtReturn;
}